自动获取某段代码的浮点运算计数的方法 [英] Automatic way to obtain the floating-point operation count for some piece of code
问题描述
我有一些相当复杂且高度模板化的代码(C ++,但这可能不是很相关),我想知道其中的add,subs,muls,div和sqrts的数量.有没有一种自动的方式来获取这些信息(编译器可以很容易地计算出来)?我试图自己在生成的汇编代码中对其进行计数,但对jp
,jmp
和call
s感到困惑.
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 call
s.
推荐答案
对于某些类似浮点的类型,我建议覆盖+
,-
,*
,/
运算符和sqrt
函数,您可以在其中计算出它们的用途.
I would suggest to override +
, -
, *
, /
operators and sqrt
function for some float-like type, in which you can count their use.
类似这样的东西:
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>();
这篇关于自动获取某段代码的浮点运算计数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!