I have the following two loops:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
int start=clock();
for (int i=0;i<100;i++)
cout<<i<<" "<<endl;
cout<<clock()-start<<"\n"<<endl;
cout<<"\n";
int start1=clock();
for (int j=0;j<100;++j)
cout<<j<<" "<<endl;
cout<<"\n";
cout<<clock()-start1<<" \n "<<endl;
return 0;
}
I ran that three times. On the first two runs, the second loop was fastest but, on the third run, the first loop was fastest. What does this mean? Which is better? Does it depend on the situation?
In your case it is probably standard measurement error and it does not matter do you use post-increment or pre-increment. For standard types (int, byte …) it does not matter.
BUT you should get used to using pre-increment because there are performance implications if you are using them on a Class, depending how those operators are implemented. Post-increment operator i++ has to make a copy of the object
For example:
Take a look at the following answer
StackOverflow: i++ less efficient than ++i, how to show this?