I thought always, that it must be conversely. But when I tried this simple code, I got unexpected results:
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <chrono>
using namespace std;
int main(int argc, char* argv[])
{
int x = 0, y = 0;
double z;
chrono::steady_clock::time_point start_point;
start_point = chrono::steady_clock::now();
for(int i = 0; i < 100000; x = ++i)
for(int j = 0; j < 100000; y = ++j)
z = static_cast<double>(x * y);
cout << "The prefix increment took a " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start_point).count() << " milliseconds" << endl;
start_point = chrono::steady_clock::now();
for(int i = 0; i < 100000; x = i++)
for(int j = 0; j < 100000; y = j++)
z = static_cast<double>(x * y);
cout << "The postfix increment took a " << chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now() - start_point).count() << " milliseconds" << endl;
// To make the compiler happy...
x = y = static_cast<int>(z / z);
cout << "SUCCESS" << endl;
return EXIT_SUCCESS;
}
The result of running of this code at my machine is:
The prefix increment took a 25716 milliseconds
The postfix increment took a 19119 milliseconds
SUCCESS
EDIT:
Yep, changing the z = ...
to the z += ...
leaded to that the execution times became equal.
So thank all of you for your answers.