From: Dan Sugalski Date: 2003-01-06T14:10:17+09:00 Subject: Re: Compile time constant folding? At 11:25 AM +0900 1/6/03, Tim Bates wrote: >On Mon, 6 Jan 2003 12:45 pm, Dan Sugalski wrote: >> >Anyway, why would one have such a complex expression within a loop? >> >> It's not uncommon to have expressions that look non-constant that >> turn out to really be constant once surrounding code's analyzed. > >Shouldn't it be the programmer's responsibility to find these cases and take >them outside of the loop while optimising eir code? It's been my unpleasant experience that any statement that starts "Shouldn't it be the programmer's responsibility" generally has as its second sentence "Unfortunately they don't so often that it's worth having the computer do it instead." There's an amazing amount of redundancy that can be removed from a program that you didn't realize was there in the first place, and it doesn't take long for even a lightly maintained program to end up with soggy bits. The compiler is also the only thing that ever truly has a complete view of your program, if anything ever does, and so is in a position to do things at compile time that may only be valid for that compilation because libraries change or whatever. Besides, programmers are human and humans have a limit, often a very small limit, to the number of things they can juggle in their heads at once. Computers are awfully stupid in many ways, but they can juggle a lot of things simultaneously. For a painful lesson, find a piece of well-written C code. (It isn't tough, really) Run it through the compiler, once with optimizations on just a little (not even the really aggressive ones), and once with them off. You'd be amazed at the speedup you get from even simple things. Yes, C isn't Ruby/Perl/Python/Scheme/Whatever. Still, the comparison is valid. -- Dan --------------------------------------"it's like this"------------------- Dan Sugalski even samurai dan@sidhe.org have teddy bears and even teddy bears get drunk