Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!decwrl!deccrl!bloom-beacon!eru!hagbard!sunic!dkuug!diku!juul From: juul@diku.dk (Anders Juul Munch) Newsgroups: comp.lang.c Subject: Re: low level optimization Message-ID: <1991Apr11.150858.4242@odin.diku.dk> Date: 11 Apr 91 15:08:58 GMT References: <1991Apr9.213601.12309@agate.berkeley.edu> Sender: news@odin.diku.dk (Netnews System) Organization: Department of Computer Science, U of Copenhagen Lines: 64 rkowen@violet.berkeley.edu (Dario Bressanini) writes: >>> Consider for a moment (yes, these are not equivalent): >>> >>> x = ++c; vs x == c++; Of course you mean x = c++; - right? >>> These can be "compiled" as: >>> >>> temp_001 <-- c; >>> c <-- c + 1; c <-- c + 1; >>> x <-- c; x <-- temp_001; >>> >>> (now throw away the "x=" part (last "instruction"). >>> >>>So, "++c" is ``cleaner'' in some pedantic sense[1], and I suppose a >>>sufficiently lacking compiler might actually produce slower code >>>for "c++;" than for "++c;". ^^^^^^^^^^^^^^^^^^^^ >I always read this group with interest, i have learned a lot following >many discussions, but sometimes the "war" "This statement is faster >than the other" comes up, like in this case. I don't want to discuss >this particular case, but to make some general personal observations. >Based on my experience, I hardly believe that in a "real world code" >one can improve the performances of a program using this kind >of "optimization". >[stuff deleted] You're probably right, this is hardly a worth-while optimization. He does have a point, though, in that post-increment/decrement is a rather clumsy construct from a compiler point of view. As long as it is such a simple statement, any compiler can sort out how to do it the fastest way, anyway. But when the increment is in the middle of some assignment or array index, it may not be able to do that. I once read saw an implementation of quicksort in a magazine, which contained code like: array[index++] = something; They stated it was written that way for efficiency. I tried compiling that code, together with the rewrite array[index] = something; index++; And then I looked at the code that my "real-world" compiler (Turbo C++ 1.0) generated for these two code fragments - Identical! The morale of the story is, don't use increment/decrement-operators within other expressions - it makes the code less readable and it's probably doesn't give you any performance gain anyway. - Anders Munch, juul@diku.dk /------------------------------------------------------\ | (Insert your disclaimer of choice here) | | | \------------------------------------------------------/