Which is faster post increment or pre increment ? and in
which cases should u use either - to increase speed?

Answers were Sorted based on User's Feedback



Which is faster post increment or pre increment ? and in which cases should u use either - to incr..

Answer / me

In normal cases where we use x++ or ++x for integer
variables in loops etc, both behave the same.

However, when we have classes that overload the ++
operator, it's faster to use the ++x rather than x++.

This is because when we do x++, a temporary object is
created to point to the original value, then the value is
incremented, and the pointer is updated and returned.

in case of ++x, just the value is incremented and pointer
to itself is returned. therefore ++x is faster in this case.

Is This Answer Correct ?    31 Yes 1 No

Which is faster post increment or pre increment ? and in which cases should u use either - to incr..

Answer / mms zubeir

The above answer seems to be correct but for normal cases
also the behavior is as explained, it is not only for
overloaded case.

A little deeper, since a temporary object is introduced to
swap older and newer values, extra copying is required
which swallow its own CPU time. So the post increment
operator is a bit slower.

But this difference is feeble.

Is This Answer Correct ?    16 Yes 1 No

Which is faster post increment or pre increment ? and in which cases should u use either - to incr..

Answer / bakeur

the faster is pre increment

Is This Answer Correct ?    0 Yes 0 No

Which is faster post increment or pre increment ? and in which cases should u use either - to incr..

Answer / zuber

extreme stage

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More OOPS Interview Questions

What is a template?

7 Answers  


What is solid in oops?

0 Answers  


Can private class be inherited?

0 Answers  


what is a binary overloading

2 Answers  


What is polymorphism and types?

0 Answers  






#include <iostream> using namespace std; int main() { int a = 3; int c[5][5]; for (int x=0;x<5;x++) { for (int y=0;y<5;y++) { c[x][y] = x*y; } } cout << c[a][2]; }

1 Answers  


What is a friend function & its advantage?

1 Answers   MIT,


What is Inheritance, Multiple Inheritance, Shared and Repeatable Inheritance?

4 Answers   Accenture, L&T,


What does sksksk mean in text slang?

0 Answers  


How to create a comment page in C #??

2 Answers  


why c++ is a highlevel language

3 Answers   Satyam, Tech Mahindra,


Why and when is a virtual destructor needed?

5 Answers  


Categories