-5

++i is supposed to be faster than i++ right?? Then why this o/p ?? Below is the code in CPP

start = clock();
srand(time(NULL));

cout<<"++i"<<endl;
for(int i=0;i!=-1;++i);

end = clock();
total_time = ((double) (end - start)) / CLOCKS_PER_SEC;
cout<<"Time taken for ++i : "<<total_time<<"\n\n";

start = clock();
srand(time(NULL));

cout<<"i++"<<endl;
for(int i=0;i!=-1;i++);

end = clock();
total_time = ((double) (end - start)) / CLOCKS_PER_SEC;
cout<<"Time taken for i++ : "<<total_time<<endl;

And this is the output

++i Time taken for ++i : 12.2812

i++ Time taken for i++ : 12.125

  • 2
    *"++i is supposed to be faster than i++ right?"* - That argument doesn't apply to primitive types like `int`. Only user defined types. And the argument is that it can (should?) never be slower than, not that it is always faster than. – Galik Oct 22 '19 at 07:19
  • 4
    Possible duplicate of [Is there a performance difference between i++ and ++i in C++?](https://stackoverflow.com/questions/24901/is-there-a-performance-difference-between-i-and-i-in-c) – Gor Asatryan Oct 22 '19 at 07:20
  • Try to implement them and you will understand why it should be faster. Also, use a high resolution clock and compile without optimizations. – zdf Oct 22 '19 at 07:22
  • 2
    @ZDF **with** optimizations? – Benjamin Bihler Oct 22 '19 at 07:25
  • 3
    First of all: overflowing signed integers is an undefined behaviour. Secondly: any sane compiler will produce the same assembly for your code (which you can check here: https://godbolt.org/z/esZ0PR ). Thirdly: compilers may and will optimize out dummy loops like yours when optimizations are enabled, your code is flawed. Finally: you are measuring clock ticks. It is the smallest unit of time recognizable by a device. Ergo everything after `.` is a approximation noise. – freakish Oct 22 '19 at 07:26
  • @BenjaminBihler Without. – zdf Oct 22 '19 at 07:50
  • 3
    @ZDF But speed measurements without optimization are nonsense. Why would you measure unoptimized binaries?!?! – Benjamin Bihler Oct 22 '19 at 07:56
  • @BenjaminBihler Read Batsheba's answer below. – zdf Oct 22 '19 at 09:48
  • @ZDF Batsheba is right: for speed measurements you have to write code that cannot be optimized away by the compiler. Still, you never want to measure unoptimized binaries. Otherwise the numbers just have no useful meaning! – Benjamin Bihler Oct 22 '19 at 09:58
  • @BenjaminBihler The question is "Why...". – zdf Oct 22 '19 at 10:04
  • @ZDF Because most probably you will compile your software in release mode with optimizations turned on while deploying it. Therefore this performance is important and not the performance in debug mode during the development phase. – Benjamin Bihler Oct 22 '19 at 10:10

2 Answers2

4

Indeed conceptually at least ++i will never be slower than i++ due to the latter somehow storing the original value for later return.

But you are forgetting an important concept: the as-if rule. All modern compilers will optimise ++i and i++ to the same thing.

Note furthermore that a good compiler will optimise the undefined statement (due to int overflow) for(int i=0;i!=-1;++i); to a no-op. Many would also do so if i was an unsigned type. This is because its inclusion or otherwise has no effect on the program.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

clock () method is not a good way to calculate the time difference

maybe you can use

high_resolution_clock::now()
Yaser Darzi
  • 1,480
  • 12
  • 24