Nit-Picky.

Started by
10 comments, last by Taulin 23 years, 11 months ago
I have read a lot of code here about people searching for speed in very small things, but they still seem to miss even more minute details that might not make a difference. Here is one: ++i is faster that i++ i++ has to make another instance of i''s type and give it the original value of i and return the value of the old instance. Where as ++i simply just adds one. Does anyone else have any small things that are small, but are leathal? Phil
Advertisement
++i does something else completely to i++, the two are not interchangeable, therefore a comparison like that is kindof pointless.
And another thing, if you''re picking your code apart that much, you''ve got too much time on your hands!!

#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
++i is by no means a silver bullet of optimization. In fact, in many cases, it makes no difference at all to use i++ instead of ++i.

Tim
quote:Original post by MadKeithV

++i does something else completely to i++, the two are not interchangeable, therefore a comparison like that is kindof pointless.


are you telling me that:
for (int i=0; i<10; ++i)
is different to:
for (int i=0; i<10; i++)
?

In this case, it''ll probably make no difference whatsoever. But if you used a complex type (eg. some sort of iterator class), the compiler may not be able to optimise them down to the same thing.
quote:Original post by Kylotan
are you telling me that:
for (int i=0; i<10; ++i)
is different to:
for (int i=0; i<10; i++)
?

In this case, no difference because the value of the expression is not being used, but the fact remains that during assignment ++i == i + 1, and i++ == i





#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Yes, they do different things, that is what I was describing. But their different methods should be considered when choosing which one to use.

In the ''For'' loop example, ++i should always be the choice. A for loop with 100 iterations, and using
i++ is creating an i type 100 times. At each iteration, creating another instance. Which do you think
is more efficient?
And if you consider contemplating internal operation of the C++ language as ''Having to much time on your hands'', then you need to have a good code review.

And on the side, I didn''t call this thread ''Nit-Pic'' for nothing. But I think it is cool that people responded to it.
quote:
At each iteration, creating another instance. Which do you think is more efficient?


Have you actually tested this (Debugged it or timed your code) or are you just saying something that sounds reasonable. To me, it makes more sense that any decent compiler would rearrange the code so that the increment comes after the old value of i is used.

Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
I looked into the code generated by VC++. I compiled as Debug version, so there should be no great optimization. Here's what I got:

396: for(i=0; i < n; ++i)
00432E3A mov dword ptr [ebp-14h],0
00432E41 jmp ISEEDIT::CTEXTURE::Restore+8Ch (00432e4c)
00432E43 mov eax,dword ptr [ebp-14h]
00432E46 add eax,1
00432E49 mov dword ptr [ebp-14h],eax
00432E4C mov ecx,dword ptr [ebp-14h]
00432E4F cmp ecx,dword ptr [ebp-18h]
00432E52 jge ISEEDIT::CTEXTURE::Restore+161h (00432f21)

And the other version:

396: for(i=0; i < n; i++)
00432E3A mov dword ptr [ebp-14h],0
00432E41 jmp ISEEDIT::CTEXTURE::Restore+8Ch (00432e4c)
00432E43 mov eax,dword ptr [ebp-14h]
00432E46 add eax,1
00432E49 mov dword ptr [ebp-14h],eax
00432E4C mov ecx,dword ptr [ebp-14h]
00432E4F cmp ecx,dword ptr [ebp-18h]
00432E52 jge ISEEDIT::CTEXTURE::Restore+161h (00432f21)

I don't think that VC++ is the only compiler that generates the same code in both cases. However I'll check this out with gcc.

Edited by - VolkerG on May 8, 2000 12:35:36 PM
++I is a pre-increment, and i++ is a post-increment.

They are identical, except for when it comes to the order of operations in an expression:

If you call someFunc(I++); the value of I will be sent to someFunc() and afterwards incremented. If you call someFunc(++I); the value of I will be incremented, then the sent to the function.

What''s fun with using post and pre operators is that you can use them as l-values, letting you do neat things in loops.
Creativity is a bloody nuisance and an evil curse that will see to it that you die from stress and alcohol abuse at a very early age, that you piss off all your friends, break appointments, show up late, and have this strange bohemian urge (you know that decadent laid-back pimp-style way of life). The truly creative people I know all live lousy lives, never have time to see you, don't take care of themselves properly, have weird tastes in women and behave badly. They don't wash and they eat disgusting stuff, they are mentally unstable and are absolutely brilliant. (k10k)
By "neat", of course, you mean making it impossible for anyone else to read your code.

This topic is closed to new replies.

Advertisement