C++ don't have "instructions" but statements (and it is unclear why i++, j++;
-which is a single statement with a comma operator- should be counted differently than i++; j++;
and both could have the same semantics and are likely to generate the same code in most compilers....)
(so your question is unclear, and probably makes no sense)
And a C++ compiler could optimize your code (using the as-if rule) into the equivalent of
c += 2;
d += 2;
which has the same observable behavior as your code chunk. Then what "statement count" do you want to measure? Loop unrolling is a very common optimization (that most compilers would do).
At last, on modern CPUs (like those in your laptop), the execution time is not much related to the number of C++ statements (or even of machine code instructions) executed, because of cache issues, branch predictors, instruction pipelining, superscalar processors, etc...
Perhaps you want profiling tools. On Linux, if you compile with g++ -pg -O
then use gprof(1) (but look also into perf(1)), you get something close to what you want.
(I am not sure your question makes real sense; what makes much more sense is the measured time of some benchmark, that you repeat several times and that you make long enough - at least half a second in practice.)
Read also about Worst Case Execution Time (WCET). There are tools related to it.
Remember that time complexity is asymptotic. It cares about timing for arbitrarily large input sizes, and the notion of limit and of convergence is essential when you speak of O(n).
Don't forget that optimizing compilers are changing your code in some significant way. See CppCon 2017: Matt Godbolt talk: “What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid” (which also give hints on why in practice a decent C++ compiler has to make a lot of optimizations; without them C++ would be practically useless).
If you are ready to spend months to get a more or less generic solution to your question, you could design and make some GCC plugin to add instrumentation code into your program. I really think it is not worth the effort, but you are free to try.
Part of the difficulty of such an hypothetical GCC plugin is to measure precisely and unambiguously what are the statements in every C++ code - including C++ code using standard containers which most genuine C++ code use .... You'll find out that it is not simple.... what would be exactly the dynamic statement count -in your ill-defined sense- of some genuine C++ code using <vector>
like std::vector<int> v(10); v.push_back(2);
?? Your measure would probably be specific to some particular implementation (e.g. the GCC one) of the C++ standard library.
(and that is another reason why your question does not make any sense; notice that on my Linux desktop #include <vector>
expands to nearly ten thousand lines, but most of the statements there get optimized and removed as dead code)
If you just want to get measures for one particular program, it is simpler (but still useless) to manually add some global counter increment in each of your basic blocks. If you adopt a particular but verbose -so hard to follow- C++ coding style where each basic block is syntactically a block in braces {}
, that could be easy, and perhaps might be done automatically (at least if you don't use most of C++ features, like its containers).
But I recommend to use existing profiling, code coverage, benchmarking and timing tools (e.g. on Linux gprof
, perf
, gcov(1), time(1) etc...; see also time(7))