Search code examples
c++cmetricscode-metricsflops

Automatic way to obtain the floating-point operation count for some piece of code


I have some rather complex and highly templated code (C++, but this may not be very relevant) of which I'd like to know the number of adds, subs, muls, divs, and sqrts at execution. Is there an automatic way to get this information (the compiler could work it out easily)? I tried to count it myself in the assembler code generated, but got confused with jp, jmp, and calls.


Solution

  • I would suggest to override +, -, *, / operators and sqrt function for some float-like type, in which you can count their use.

    Something like this:

    struct Double {
        double val;
        Double(double v): val(v) {}
        static unsigned add_count = 0;
        Double operator+(Double other) {
            ++add_count;
            return Double(val + other.val);
        }
    };
    
    do_your_stuff<Double>();