low level optimization

Anders Juul Munch juul at diku.dk
Fri Apr 12 01:08:58 AEST 1991


rkowen at 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 at diku.dk
/------------------------------------------------------\
|      (Insert your disclaimer of choice here)         |
|                                                      |
\------------------------------------------------------/



More information about the Comp.lang.c mailing list