i plus plus

Jeffrey Traer Bernstein jeffyb@ccrma.Stanford.EDU
Tue, 13 May 2003 23:14:26 -0700


There is no speed difference in + or - on a 2-complement machine 
(anything made since we were born)
and there is no speed difference in while and for, their machine-code 
translations are the same.
greater/less-than and == 0 should both be 1 cycle as well.

This is nothing to worry about. At most any of this makes a 1 cycle 
difference. Just a tip. If you get a coding question in an interview you 
can use it in your answer and impress them (not that you should listen 
to me about getting a job, maybe talk to dave :)

This does not effect when the increment is performed in a loop!!!!!!

for ( A, B, C )
D

means
do A
check B
do D
do C
check B
do D
do C
and so on....

regardless of which increment operator you use!

No more of your insolence! Apparently my "tips" are unwanted! Hmmmmph.

Lectures to come:
Design Patterns
Object Oriented Cooking
Object Oriented Theories of Microwave Repair & Carpentry
Better living through loop unrolling
Time Travel
Space Ship Dimensions

disclaimer: none of the above constitues legal advice or counsel, any 
and all remarks are for the purpose of discussion and do not reflect my 
opinions or those of the Center for Computer Research in Music and 
Acoustics or The Leland Stanford Junior University and Mexican 
Restaurant & Taqueria (Established 1891)





David Lowenfels wrote:

> for( i=0; i<10; i++ ) {;}  //this is slow but very clear
>
> for( i=0; i<=10; ++i ) {;} //this is faster
>
> or if you don't need an index, just a repeat:
>
> i = 10+1; //loop 10 times... sorta confusing
> while( --i ) {;}    //this is fastest?
>
> I think maybe people use i++ becuase it is semantically simple... no 
> weird extra number stuff.
>
> -DFL
>
>> You got it backwards again...
>> ++i is faster than i++
>>
>> ++i  is pre-assignment-increment, in other words increment and then 
>> assign the value
>> i++ is post-assignment-increment, in other words assign the value 
>> then increment.
>>
>> so that means every time you use post-increment a temporary variable 
>> holding the present state of the variable is created so that it can 
>> be returned and the actual value incremented, this is the semantic 
>> definition and how it must be implemented in any class.
>>
>> as an example:
>>
>> ++i
>> {
>> i = i + 1;
>> return i;
>> }
>>
>> and ...
>>
>> i++
>> {
>> int j = i;
>> i = i + 1;
>> return j;
>> }
>>
>> so....
>>
>> int i = 0;
>> cout << i++;
>>
>> output = 0
>>
>> and...
>>
>> int i = 0;
>> cout << ++i;
>>
>> output = 1;
>>
>> this behaviour only comes into play with assignments so since you 
>> have the pick of the two in loops you should choose the faster one 
>> which is always ++i. I have no idea why i++ is in every basic loops 
>> example in every loop. I guess it must have been in a basic example 
>> in the Kernigan-Ritchie book (the first book on C, by the authors of 
>> C, also where that "Hello world!" example program now the standard in 
>> ALL programming text books comes from!)
>>
>> jeffyb
>>
>>
>>
>>
>> Hiroko Terasawa wrote:
>>
>>> Thanks Jeffyb,
>>>
>>>
>>>> use ++i in loops instead of i++, it's pre-increment so it's faster
>>>>  
>>>
>>> I use i++ for coding, but when writing ...
>>> ...snip...
>>> increment? Why does it make the code faster?
>>>
>>> ps. I found a treat for you...
>>> http://mirror.wolffelaar.nl/zardalu.sytes.net/
>>
>>
>> _______________________________________________
>> mamst mailing list
>> mamst@ccrma.stanford.edu
>> http://ccrma-mail.stanford.edu/mailman/listinfo/mamst
>
>
>