Richard said:
You think correctly. The "as if" rule conquers all.
Humm, this just got me thinking. Let's say we have this code
int i;
i = 50;
printf("Hello world.\n");
return i;
Obviously, any compiler would optimize that to
printf("Hello world.\n");
return 50;
As there would be no observable difference. So say we have code like this:
int i;
i = unknownlibrarycall();
printf("Hello world.\n");
return i;
Where unknownlibrarycall is some library the compiler does not know
anything about except for the prototype:
int unknownlibrarycall(void);
It happens to be
int unknownlibrarycall() {
return 50;
}
Would the rearrangement to
printf("Hello world.\n");
return unknownlibrarycall();
be legal here (although it would probably not matter at all, but for
academic purposes).
And, if you exaggerate the problem;
int i, j;
i = 1;
for (j = 0; j < 2000000; j++) i *= j;
printf("Hello world.\n");
return i;
Say the loop takes a *long* time, is the code *really* the same as
int i, j;
printf("Hello world.\n");
i = 1;
for (j = 0; j < 2000000; j++) i *= j;
return i;
My point is: does timing account to "observable difference"? In some
circumstances it obviously doesn't - optimization is okay, although the
difference is (obviously!) observable. In some circumstances (like the
last one) it is an observable difference, as the "Hello world" is
printed one case at the beginning of the program and in the other at the
end.
Just thinking... happy to hear your comments.
Regards,
Johannes