OK, here is some real-life testing on my computer:
Test1.cpp:
int foo() { int x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; x = x % x; } int bar(void) { return 0; } int main() { bar(); return 0; }
Test2.cpp:
int bar(void) { return 0; } int main() { bar(); return 0; }
Tested for 10,000 times (CPU cycles measured):
Test1.cpp 6.044 s Executable size: 7,355 bytes Test2.cpp 6.036 s 7,331 bytes
All in all, it seems like a small performance boost can be achieved, but my results I would chalk up to negligible inefficiencies. Keep in mind, though, that this is without any of the built in libraries. I can guarantee that they will be more code heavy than 50 modulus operations.
Part 2:
Test3.cpp:
#include <stdio.h> #include <stdlib.h> int bar(void) { return 0; } int main() { bar(); return 0; }
Test4.cpp:
int bar(void) { return 0; } int main() { bar(); return 0; }
Results
Test3.cpp: 5.960 s Executable size: 7331 bytes Test4.cpp: 6.176 s, 5.964 s, 6.116 s Executable size: 7331 bytes
It seems like the compiler does a pretty good job of dealing with these things. After including some larger libraries, it seems like they are removed from the file.
#if 0...#endifinstead. For that matter, if you use a series of#ifdefs, you can define a symbol on the compiler's command line to control which function is compiled (which may or may not be a good thing).