Skip to content

Pre-increment vs. Post-increment: Which is Faster? (Part II)

ADMIN | 15-02-2011

In the end, I’m okay if you use pre-increment – but for myself, I’ll aspire to loftier programmatic governances.

Hexagon pattern 1

By Kevin Tatterson

In my previous post I explored the notion that ++d is faster than d++. 

Ponderings

Now for an educated guess on what would happen if we got rid of the __asm nop and allowed the optimizer to inline. At the very least, the instructions in dark red in the previous example (lea, call myint::operator++, nop, and ret) would go away, leaving us with 8 clock cycles for pre-increment and 11 clock cycles for post-increment: which would make pre-increment is 27% faster!

Back to reality for a moment. In actuality, the myint example gives the best case figures because of two reasons: both the myint copy constructor and the pre/post-increment’s are dead simple – one clock cycle each – and inlining works because the implementations are short. So what happens if these implementations get even just a little more complex?

Cost (clocks)

 

 

of copy ctor & operator++

Pre-incr

 

 

Total

Clocks

Post-incr

 

 

Total

Clocks

% Faster
2 (best case, our example)                   8                                  11                               27%                        
10 16 19 16%                     
20 26 29 10%
40 46 49 6%

 

Now consider that seemingly innocuous instructions will explode the number of clock cycles into the 100’s and 1000’s – calls like sprintf, malloc, new, itoa – will blow this example out of the water and reduce the benefit to nil.

Conclusion

I have mixed feelings on whether to recommend pre-increment over post-increment:

  • Your copy ctor and pre/post-incr implementation have to be dead simple to measure a win.
  • It wouldn’t surprise me if compiler optimizers are able to determine when post-increment can be replaced with pre-increment, when your program’s semantics allow.
  • It doesn’t change the semantics of your program much, but other developers might wonder why you favor pre-increment.
  • In the grand scheme of things, few real world algorithms’ performance will measurably affected by favoring pre-increment.

Here at Spatial, I’d like to think that we take a pragmatic approach to our software’s performance. CGM, ACIS, 3D InterOp, and IOP-CGM, rarely give concern to this level of minutia. I’d like to describe our approach to performance as tactical and pareto-ized. As I said, in rare instances we give concern to minutia, but only when our profilers tell us to.

In the end, I’m okay if you use pre-increment – but for myself, I’ll aspire to loftier programmatic governances. What are these governances? That’s another blog – one that is sure to stir things a bit.

You might also like...

5 Min read
CGM Modeler
Software components are like the stage crew at a big concert performance: the audience doesn’t see them, but their...
8 Min read
CGM Modeler
There’s a lot of confusion around what the terms additive manufacturing and 3D printing mean.
2 Min read
CGM Modeler
WRL files are an extension of the Virtual Reality Modeling Language (VRML) format . VRML file types enable browser...
Voxel model example
3 Min read
CGM Modeler
Voxels are to 3D what pixels are to 2D. Firstly -- let’s examine what pixels actually are. Everything you see on your...
Point_cloud_torus
2 Min read
CGM Modeler
Point-cloud modeling is typically used in the process of 3D scanning objects. Rather than defining surfaces through...
Polygonal Modeling
2 Min read
CGM Modeler
Polygonal (or polyhedral) modeling is the most common type of modeling for video games and animation studios. This type...
BREP Model example
2 Min read
CGM Modeler
BRep modeling, or Boundary Representation modeling, is, in CAD applications, the most common type of modeling. BRep is...
Construction site with crane and building-1
3 Min read
3D Software Development Kits
Digital twins are changing the way the construction industry manages large building projects and infrastructure...
3 Min read
3D Software Development Kits
There is a new wave of innovative processes and solutions that improve product production throughput, and enable...
3D Product Visualization.jpg
2 Min read
3D Software Development Kits
In the 3D modeling space, application developers face a number of challenges — getting to market on time, delivering...
General PMI
4 Min read
3D Software Development Kits
It has already been a year since release 2017 1.0, which saw the introduction of a number of innovative features aimed...
Large Tolerant  Vertex
3 Min read
3D Software Development Kits
boolean operation Boolean operations on individual bodies are common functions in 3D modeling. While simple in concept,...
3 Min read
3D Software Development Kits
In much the same way as physical design has moved from paper 2D drawings to 3D models in software, so has analysis....
2 Min read
3D Software Development Kits
Today was day one of Spatial Corp.’s 3D Insiders’ Summit 2016, an educational event that focuses on technical topics...
4 Min read
3D InterOp
Part and parcel with model-based engineering is model translation. Because the model is now the specification, accurate...
3 Min read
3D Software Development Kits
In case you missed the news this week, we announced the availability of Release 2017 1.0 for Spatial software...
2 Min read
3D Software Development Kits
Augmented World Expo 2016
4 Min read
3D Software Development Kits
Make or buy is an age old question for anyone building products, whether hardware or software. For example, for a...