I was profiling the pre vs post increment operator in C (out of curiosity, not for micro-optimization purposes!), and I got some surprising results. I expected the post increment operator to be slower, but all of my tests show that it's (non-trivially) faster. I've even looked at the assembly code generated by using the -S
flag in gcc, and post has one extra instruction (as expected). Can anyone explain this? I'm using gcc 4.8.1 on Arch Linux.
Here is my code.
EDIT: There is an integer overflow error in my code. It doesn't effect the question, but you should note the actual number of iterations is different from the argument passed in.
Post.c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("Missing required argument\n");
exit(1);
}
int iterations = atoi(argv[1]);
int x = 1;
int y;
int i;
for (i = 0; i < iterations; i++) {
y = x++;
}
printf("%d\n", y);
}
Pre.c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("Missing required argument\n");
exit(1);
}
int iterations = atoi(argv[1]);
int x = 1;
int y;
int i;
for (i = 0; i < iterations; i++) {
y = ++x;
}
printf("%d\n", y);
}
Here are the results:
$ gcc post.c -o post
$ gcc pre.c -o pre
$ I=100000000000; time ./post $I; time ./pre $I
1215752192
real 0m2.777s
user 0m2.777s
sys 0m0.000s
1215752193
real 0m3.140s
user 0m3.137s
sys 0m0.003s
I also wrote a timing script a while ago. Running that gave the same results:
$ I=100000000000; comptime "./pre $I" "./post $I"
3193 3133 3157 3143 3133 3153 3147 3150 3143 3146 3143
2743 2767 2700 2727 2700 2697 2727 2710 2680 2783 2700
Mean 1: 3149.18
Mean 2: 2721.27
SD 1: 6.1800
SD 2: 21.2700
The output is mostly self explanatory, but the idea is that it runs both programs 10 times (by default) and calculates the mean and standard deviation of the results in milliseconds.
I've included the assembly code generated by gcc for post.c and pre.c on pastebin, since I didn't want to clutter this post with things that might be unnecessary.
Sorry for bringing this debate up again, but these numbers just seem strange to me.