i plus plus

John McCarty mccarty@stanford.edu
Tue, 13 May 2003 22:59:14 -0700


With our modern super mondo ultra powerful lighting fast computers,
we can stop discussing operator efficiency like a bunch of 70s
mainframe nerds, Jeff really want's to lecture us all about
design patterns anyway!!


At 10:46 PM 5/13/03, 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
>
>
>--
>_/^\_/^\_/^\_/^\_/^\    Science is but a perversion of itself unless
>   David Lowenfels       it has as its ultimate goal the betterment
>  Vibrational Engineer   of humanity. -- Nikola Tesla
>^\_/^\_/^\_/^\_/^\_/^            http://www-ccrma.stanford.edu/~dfl/
>_______________________________________________
>mamst mailing list
>mamst@ccrma.stanford.edu
>http://ccrma-mail.stanford.edu/mailman/listinfo/mamst