Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Taulin

Nit-Picky.

This topic is 6927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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**

Share this post


Link to post
Share on other sites
++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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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**

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
++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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
By "neat", of course, you mean making it impossible for anyone else to read your code.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!