Performance difference between ++iterator and iterator++?

后端 未结 7 1065
悲哀的现实
悲哀的现实 2020-12-08 04:32

My workmate claims that for object types preincrement is more efficient than post increment

e.g.

std::vector vec;

... insert a wh         


        
相关标签:
7条回答
  • 2020-12-08 05:30

    For primitive types, I used to do this. It used to get me about 10% boost in many programs on Visual Studio back in 1998/1999. Shortly after that, they fixed their optimizer. The post increment operator created a variable on the stack, copied the value, incremented the source, and then removed the variable from the stack. Once the optimizers detected it was not being used, the benefit went away.

    Most people did not stop coding that way. :) It took me a few years.

    For objects, I am not sure how that would work. I assume a copy operator would be required for a true implementation of post increment. It would have to make a copy and use the copy for the operations, and increment the source.

    Jacob

    0 讨论(0)
提交回复
热议问题