Sign in to follow this  
Nostalgic_Influence

Very Basic Optimisation (pre or post increment)

Recommended Posts

Hi, probably a very stupid question but anyway:

My lecturer told me that this "++i" is faster than "i++" when incrementing a for loop. Can anybody argue this point?

And if it's true, could you possibly explain why it's faster? 

Cheers

Share this post


Link to post
Share on other sites

It depends on the type of i. If i is an integer, your compiler will likely generate the exact same code either way. If i is a complex type like an iterator with range checking, then ++i may be faster. Basically, ++i just increments i. i++ requires a copy to be made of i and then the object is incremented before returning the old copy. For integers, its very easy for the compiler to realize that the copy doesn't matter. For complex type it may or may not be able to decide that. 

Share this post


Link to post
Share on other sites

I've noticed that the VS compiler tends to be more aggressive about eliding the copy when it's the "iterate" argument of a for loop. I don't remember what version it was that I was looking at, though.

It's not a bad habit to just use the prefix operator when you're not capturing the result, though.

Share this post


Link to post
Share on other sites

I've never seen a case where ++i was slower than i++.  At worst case they're equal.  So until I see otherwise in a profiler I always use ++i.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627738
    • Total Posts
      2978881
  • Similar Content

    • By JimsonLima
      Hey devs!
      Want to get rid of all the bugs in your game?
      I co-own a video game QA company called Level_0 Studios, we perform professional QA testing for game developers. Our goal is to help you create bug free games your players will enjoy. Partnering with Level_0 allows you to focus more time towards game development while we find those pesky bugs.
      If you’re interested and in need of professional game testers contact us at contact@level0studios.com and check out our website at https://level0studios.com for more information.
    • By Geometrian
      Hi all,
      More than a decade ago, a problem came up on this forum for computing a fast transpose of a 3x3 matrix using SSE. The most sensible implementation stores the matrix internally as a 3x4 matrix (so, one row stores 4 elements, aligned in a vector). A version, which I believe to be the fastest currently known, was presented:
      I am pleased to report that I have been able to come up with a version which should be faster:
      inline void transpose(__m128& A, __m128& B, __m128& C) { //Input rows in __m128& A, B, and C. Output in same. __m128 T0 = _mm_unpacklo_ps(A,B); __m128 T1 = _mm_unpackhi_ps(A,B); A = _mm_movelh_ps(T0,C); B = _mm_shuffle_ps( T0,C, _MM_SHUFFLE(3,1,3,2) ); C = _mm_shuffle_ps( T1,C, _MM_SHUFFLE(3,2,1,0) ); } This should be 5 instructions instead of ajas95's 8 instructions. Of course, to get that level of performance with either version, you need to inline everything, or else you spend tons of time on moving floating point arguments to/from input registers.
      The other thing that is crucial is that the instruction set be VEX encoded. This allows generating instructions that take three arguments, like `vunpcklps`, instead of instructions like `unpcklps` that take only two. VEX is only available in AVX and higher (usually passing e.g. `-mavx` is sufficient to get the compiler to generate VEX instructions).
      -G
    • By Shaarigan
      After a feeled million hours of coding in the past 16 years there have been many ways to write code in many different languages. Some seemed correct to the time they were used, some seemed to be too strict or too chaotic and I also evolved my coding style with each new line written. Now considering the results of over 5 years in professionall game development, tools and engine code as hobbyist and on small and large commercial projects up to AAA titles, there are still many ways one could write code in different languages but also in the same language on different projects in one and the same but also different companies. I mostly agree with; see some trends in C#, C++ coding guidelines that are fully worth to go for but the major difference is on the naming conventions.
      Because I have currently to write my own coding guidelines (not for a special project but primary as a personal convention to refer to when coding) and seek for a way I'm happy with, I did some research on different guidelines and came up with following references:
      When Epic Games write about Unreal
      This seems a bit confusing when seeking for some type like Animation or Skin (that are both different prefixed with A and F) but prevents various naming conflicts to types and variables when writing a function that accepts FSkin Skin as parameter for example.
      Googles c++ guidelines point into a completely different direction when they write
      So they heavily make use of typos, underscores and also lower case prefixes to identify different kinds of member, static, nonstatic and function names and in the same breath except there rules for various special cases.
      Some other examples from different projexts I was invovled to also use and do not use prefixing types or use underscores
      class Class { const <type> cConstant; const <type> Constant; const <type> __Constant; <type> _myClassMember; <type> _MyClassMember; <type> myClassmember; <type> mMyClassMember; <type> function(<type> parameter); <type> Function(<type> parameter); <type> Function(<type> aParameter); } class NAClass //avoid using namespaces, instead prefix anything with a 2 letter namespace like identifier { ... } Dont need to mention that Visual Studio will raise a Warning/Exception that a type is named as same as a function parameter when using a class
      class Container { private int size; //current size public Resize(int size) //will cause compiler telling that type matches a member type { //do resize here } } So in the end anyone does he or she thinks that it is worth to be done and so me do too. I would like to hear your opinions to why and what codings style do you prefer or are involved to in whatever way. What do you think makes a good standard especially for the most common point, Naming Convetions?
      Will be corious to read your opinions
    • By hunpro
      Hi, I am releasing my 2d game on Steam, so sent it to review and they said it has some black and white rectangles covering parts of the screen. I don't have this issue on my old pc of course (amd 6670 vga), and I tested on a laptop too (amd gpu), without any problem. The game uses Direct3d11 with c++,  nothing fancy, 2 layer of tiles, and some sprites for decorations and some postprocess effects. I have no idea what to do. Released it a while ago on Itch.io, had some 20+ downloads, nobody said anything about not working - or anything at all, sadly. 

      So anyone does have any tips, how to figure out a graphical bug that is not reproductable on your end, and you doesn't even have a screenshot?
    • By dpadam450
      I've been starting to optimize my code in anyway possible. I saw that some cpu's actually have 256-bit SIMD, but I was wondering if there is a way to detect this and fallback to the 128-bit on an unsupported cpu, or how else to deal with this.
  • Popular Now