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

戏子无情 提交于 2019-12-13 19:34:24

问题


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.


回答1:


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



回答2:


Yes you can, but the way is a bit complex:

Try to change your "add", "sub", "mul", "div", "sqrt" in the binary to some invalid opcode. Dont forget to define an invalid opcode error handler to restore the opcode. When you program runs, the cpu will trigger the invalid opcode error at those changed "add", "sub", "mul", "div", "sqrt". By counting the times invalid opcode error being triggered, you can get the exactly what you want.



来源:https://stackoverflow.com/questions/18725289/automatic-way-to-obtain-the-floating-point-operation-count-for-some-piece-of-cod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!