Hi Geoff, Am 27.08.2015 um 08:42 schrieb galexand...@nc.rr.com: > I would like to count instructions for a specific part of my code. > ...
This suggests we should document the difference between START/STOP_INSTRUMENTAITON and TOGGLE_COLLECT better. Anyway. > for (int i = 1; i <= 1000; ++i) { > CALLGRIND_START_INSTRUMENTATION; > n += i; > CALLGRIND_STOP_INSTRUMENTATION; > } With macros in C, the compiler can reorder stuff, so you may end up with something like > for (int i = 1; i <= 1000; ++i) { > n += i; > CALLGRIND_START_INSTRUMENTATION; > CALLGRIND_STOP_INSTRUMENTATION; > } or even worse, gcc may have calculated the end result of n at compile time, and optimized the "n += i" away. Further, the use of the macros may probibit some optimizations such as loop unrolling, change register spilling and so on. These comments also are true with using TOGGLE_COLLECT, too. If you are curious, you can check resulting machine code with "objdump -S ...". In general, I would suggest to always switch measuring on/off never at this fine granularity, ie. here in the inner loop, but on a higher level. For your code, just do > CALLGRIND_START_INSTRUMENTATION; > for (int i = 1; i <= 1000; ++i) { > n += i; > } > CALLGRIND_STOP_INSTRUMENTATION; It should not really matter much whether you use these macros or TOGGLE_COLLECT. As the use of macros may change the resulting code subtly, you could rearrange your code such that you are crossing function borders at the time you want to switch measuring on/off. And then, use "--toggle-collect=<func>". Your performance issues with START/STOP_INSTRUMENTATION surprised me. It always should be faster to run in "un-instrumented" mode. If you do not use any macros at all, what is the runtime of your with vs. without using "--instr-atstart=no"? Josef ------------------------------------------------------------------------------ _______________________________________________ Valgrind-users mailing list Valgrind-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/valgrind-users