Re: How about this syntactic candy?
- From: Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sat, 24 Nov 2007 21:32:47 -0800
On 2007-11-24 18:39:31 -0800, "Steve Thackery" <nobody@xxxxxxxxxxx> said:
I agree with Arne. Do you actually *know* that list.Count gets evaluated each time and the end result is *actually* slower?
list.Count had _better_ get evaluated each time. There's nothing about C# that suggests that it should assume the value in the loop continue condition won't change. Unless the code in the loop is so simple that it is guaranteed that the list.Count property won't change during the execution of the loop, optimizing the expression to avoid reevaluating list.Count would be wrong.
I don't claim to know, but suspect there is a good chance the optimisation will sort it all out anyway. I once saw a demo of a for loop all compressed into one line with ingenious shortcuts and contractions, and after the optimising compiler had been through it, it generated EXACTLY the same machine code as an alternative loop written out in full.
There may be a situation where the compiler cannot tell that list.Count is constant, but where it actually is. In that case, I could see the utility in providing the compiler with an explicit statement to that effect, but it seems to me that the current method required is so easy and simple, I can't imagine the point in adding something extra to the language to support the behavior.
One thing I like very much about C# is its simplicity. I think people can get carried away trying to add all their favorite "syntactical sugar" to the language, weighing it down with all sorts of stuff that just makes things harder on the compiler, the spec writers, and the developers. Things that are added to the language should be _really_ important and provide some significant functionality. I don't think this example applies.
In fact, I vaguely remember someone explaining that code which is highly optimised by the writer can even make the optimising compiler produce *worse* code than a simple, but verbose, version of the algorithm. I can't remember the product, unfortunately. Could it have been an old Delphi?
I think that's a general truth, regardless of language. Optimizing compilers often can take advantage of commonly used code structures. If you write code that's "sneaky", where you've tried to create some optimization outside of the compiler, it's possible that the compiler could fail to be able to actually generate optimal code.
I would expect that to be a rare situation. Compiler technology has gotten extremely good. But I wouldn't rule it out.
The point is somewhat moot anyway. After all, you shouldn't be optimizing code that doesn't need optimizing anyway. Write it readable first, optimize if and when there's a specific performance issue that needs to be fixed.
I've always believed that optimising at the written code stage is:
1/ undesirable, because it makes the code much harder to understand, debug and maintain
2/ unlikely to work anyway
I agree. :)
Pete
.
- References:
- How about this syntactic candy?
- From: Hilton
- Re: How about this syntactic candy?
- From: Steve Thackery
- How about this syntactic candy?
- Prev by Date: Re: How about this syntactic candy?
- Next by Date: Re: How about this syntactic candy?
- Previous by thread: Re: How about this syntactic candy?
- Next by thread: Re: How about this syntactic candy?
- Index(es):
Relevant Pages
|
Loading