• Advertisement
Sign in to follow this  

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
Advertisement

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  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By shooter9688
      Once I needed a program for packing an atlas with 3d models. I could not find one, so I made it.
      Now it has only basic functionality. Should I improve it further? Does it need someone else?
       
      Link to download(for Windows): https://drive.google.com/open?id=1CLizcUAOsYnbdfyKCYDcGxmso79GPBuv
    • By standinonstilts
      Hi, I am new to Game Development and am currently making my first game in Unity using c#. I am a second year uni student studying computer science (internet security specialization). I am new to unity and have had trouble understanding how the game engine actually functions and how I should use the engine to my advantage when programming. Currently I am making a RPG and want to implement an efficient and scalable item database. My plan is to store all items in the game in an xml database using the built in unity xml serializer. I have an abstract class item -> weapon, armour, potion, ring etc. Each of these classes have respective values (damage, cost etc.). For a relatively generic and straightforward item system: How would you organize your code? What interfaces/classes/other would you implement; why? In your experience what kinds of issues have you run into and how did you work around them? Is there any other advice with regards to rpg design in general?
    • By Descent
      I need to program a game, in  task manager. How do i do it?
    • By hiya83
      (Posted this in graphics forum too, which was perhaps the wrong forum for it)
      Hey, I was wondering if on mobile development (Android mainly but iOS as well if you know of it), if there is a GPUView equivalent for whole system debugging so we can figure out if the CPU/GPU are being pipelined efficiently, if there are bubbles, etc. Also slightly tangent question, but do mobile GPU's have a DMA engine exposed as a dedicated Transfer Queue for Vulkan?
      Thanks!
    • By pabloreda
       
      I am coding the rasterization of triangles by the baricentric coordinate method.
      Look a lot of code and tutorials that are on the web about the optimization of this algorithm.
      I found a way to optimize it that I did not see it anywhere.
      I Only code the painting of triangles without Zbuffer and without textures. I am not comparing speeds and I am not interested in doing them, I am simply reducing the amount of instructions that are executed in the internal loop.
      The idea is simple, someone must have done it before but he did not publish the code or maybe in hardware it is already that way too.
      It should be noted that for each horizontal line drawn, of the three segments, you only need to look at one when going from negative to positive to start drawing and you only need to look at one when it goes from positive to negative when you stop drawing.
      I try it and it works well, now I am implementing a regular version with texture and zbuffer to realize how to add it to this optimization.
      Does anyone know if this optimization is already done?
      The code is in https://github.com/phreda4/reda4/blob/master/r4/Dev/graficos/rasterize2.txt
      From line 92 to 155
       
  • Advertisement