-1

I'm interested in counting the number of c++ instructions executed in a particular c++ program, in order to compare theoretic efficiency (that I've calculated) with efficiency in practice. Calculating theoretic efficiency I'm counting the number of instructions, that's why I need to count the total c++ instructions that the program has executed.

For example, for this code:

for(int i = 0; i < 2; i++) {
    c++;
    d++;
}

It should show me 4 instructions executed (or 6 if we count the automatic i++ on the for loop).

Is there any way to do it? I have searched related questions but I don't want to count instructions in assembly code, I want the c++ instructions count.

amritanshu
  • 777
  • 13
  • 25
Will B.
  • 23
  • 2

2 Answers2

3

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))

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • It should show 4 (or 6) too, considering that optimization. So yes, I want statement count. This is because I have calculated the O, and I want to see where the graphic (tests in practice, with different sizes of problem, n) goes, comparing it with the O function. – Will B. Apr 04 '18 at 13:38
  • 1
    What only makes sense is profiling and benchmarking. What you want does not make any sense in practice, and has no actual interest. – Basile Starynkevitch Apr 04 '18 at 13:50
  • 1
    Remember that `c++` statements don't take the same execution time. I mean 1 statement could translate to a single ASM instruction or hundreds. So even if you could count them is the count even meaningful? – drescherjm Apr 04 '18 at 13:53
  • @BasileStarynkevitch look like I understand the question better than you.. OP is saying that he needs the number of statements. hope you change your vote. – Sach Apr 04 '18 at 14:28
  • 1
    I'm trying to explain that OP's question makes no sense. You agree on that nonsense, but did not even try to explain why. – Basile Starynkevitch Apr 04 '18 at 14:35
-1

check sloccount for linux.

Also, You can use cloc utility to count lines of code.

http://cloc.sourceforge.net/

Runtime number of lines execution may vary according to input data. You can use the profiling tools to understand coverage or which lines are executed more.

Sach
  • 659
  • 8
  • 20
  • 1
    `sloccount` & `cloc` measure the source code, which is not what the OP is asking. – Basile Starynkevitch Apr 04 '18 at 13:49
  • 1
    What the OP is asking doesn't make any sense in the first place. – Rob K Apr 04 '18 at 13:58
  • @BasileStarynkevitch read the question again.... for the example. OP is expecting 4 as a result (6 as a result). So, accordingly, he is expecting a number of statement or lines of code. – Sach Apr 04 '18 at 14:17
  • 1
    But OP expects something related to some *execution* of his program. Indeed what he is asking for does not make any sense. Your answers deal only with the source code (and is unrelated to execution). In particular, `sloccount` & `cloc` both (rightly) measure [dead code](https://en.wikipedia.org/wiki/Dead_code) – Basile Starynkevitch Apr 04 '18 at 14:18
  • @BasileStarynkevitch so for that, I had suggested profiling tool ( similar to your answer) as well. but profiling will provide the different result for different input data (in general scenario). – Sach Apr 04 '18 at 14:20
  • Yes. But I guess OP wants profiling. Of course, his question is really confusing (and most of us agree that it does not make any sense) – Basile Starynkevitch Apr 04 '18 at 14:21
  • so I had provided both solutions. one is loc and another is profiling. – Sach Apr 04 '18 at 14:24
  • But still no explanation and no hints on why the OP's question makes no sense. – Basile Starynkevitch Apr 04 '18 at 14:53