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 encapsulation?

17 Answers   TCS,


Why is abstraction used?

0 Answers  


what is the difference between <stdio.h>and "stdio.h"?

5 Answers  


i got a backdoor offer in process global,Bangalore..Can i work with it?

0 Answers  


Can you name some types of inheritance?

1 Answers   Motorola,






Describe the difference between a Thread and a Process?

11 Answers   Siebel Systems,


why reinterpret cast is considered dangerous?

0 Answers   TCS,


#include <string.h> #include <stdio.h> #include <stdlib.h> #include<conio.h> void insert(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); insert(s, strlen(s)); printf("The sorted string is: %s.\n", s); getch(); return 0; } void insert(char *items, int count) { register int a, b; char t; for(a=1; a < count; ++a) { t = items[a]; for(b=a-1; (b >= 0) && (t < items[b]); b--) items[b+1] = items[b]; items[b+1] = t; } } design an algorithm for Insertion Sort

0 Answers  


Why a "operator=(...)" when there is a copy ctor?

2 Answers  


What is polymorphism and types?

0 Answers  


What makes a language oop?

0 Answers  


C#.net Interview Question A=10 B=5 C=A+B Print C The above will be given in a multiline textbox. You need to parse the above input, store values for A,B&c. And you have to display the value of C.

1 Answers   Syncfusion,


Categories