在类实例或方法的上下文中调用函数以进行性能分析
Invoke a function in context of a class instance or method for performance analysis
性能分析问题:有没有办法在类的上下文或类的方法中执行函数?
我想分析特定逻辑段的性能。我设想的是这样的
(免责声明:粗略的例子只是为了说明一点。不会编译(。
const int DEBUG_LEVEL = 7;
class PerfWrapper {
public:
PerfWrapper(int f) {} // Constructor: take function as argument
void invoke() {} // Invoke the function passed as argument
double execution_time() {
begin = std::chrono::high_resolution_clock::now();
// etc..
}
double memory_usage() {}
private:
}
int foo() {
int sum{0}
for (int i=0; i<1000; ++i)
for (int j=0; j<MAX; ++j)
sum += i * j;
return sum;
}
int main() {
if (DEBUG_LEVEL = 7)
PerfWrapper p(foo); // Create an instance, passing foo as an argument
// below foo() is called in context of the performance wrapper
int myTime = p.invoke().execution_time(); // Invokes foo in context of p and tracks execution time
int myMemory = p.invoke().memory_usage(); // Same, except gathering memory usage info.
// etc..
}
}
这里我们有类PerfWrapper。实例化后,对象上的结果方法能够接受函数作为参数,并在类的上下文中执行函数。它将进行性能测量,其结果可通过界面访问。
请注意"DEBUG_LEVEL"设置。如果需要性能分析,则只需将DEBUG_LEVEL设置为 7。
你见过这样的东西吗?如果没有,如何最好地完成分析?我知道它似乎有点离谱,但希望不是那么多。谢谢,基思:^(
也许您正在寻找函数指针,可以使用这些指针,如以下简化代码所示:
typedef int(*aFooFunctionType)(void);
class PerformanceTest {
public:
PerformanceTest(aFooFunctionType fooFuncPtr) { m_fooFuncPtr = fooFuncPtr; }
void test() {
int x = m_fooFuncPtr();
// do something with x (or not...)
};
private:
aFooFunctionType m_fooFuncPtr;
};
int fooFunc(void) {
return 100;
}
int main(int argc, char* argv[]) {
PerformanceTest pTest(fooFunc);
pTest.test();
return 0;
}
<</div>
div class="answers"> 您几乎可以将任何东西包装在std::function
中。我建议在PerfWrapper
中使用std::function
来获取执行时间。不过,我没有任何东西可以测量内存使用情况。
示例代码:
#include <iostream>
#include <functional>
#include <chrono>
class PerfWrapper
{
public:
PerfWrapper(std::function<void()> f) : f_(f), execution_time_{} {}
void invoke()
{
auto begin = std::chrono::high_resolution_clock::now();
f_();
auto end = std::chrono::high_resolution_clock::now();
execution_time_ = end-begin;
}
double execution_time()
{
return execution_time_.count();
}
std::function<void()> f_;
std::chrono::duration<double> execution_time_;
};
unsigned long foo()
{
unsigned long sum{0};
for (int i=0; i<10000; ++i)
for (int j=0; j<2000; ++j)
sum += i * j;
return sum;
}
int main()
{
PerfWrapper pr([](){std::cout << foo() << std::endl;});
pr.invoke();
std::cout << "Execution time: " << pr.execution_time() << std::endl;
}
我的设置上的输出:
99940005000000
Execution time: 0.0454077
考虑使用无模板函数,并使用引用参数来提取性能数据。 此示例将:
- 接受函数指针和函子(包括
std::function
,这意味着它也可以与方法一起使用(。 - 返回与代理函数调用返回的值相同的值,因此您可以同时使用测量数据和调用结果。
struct measurement {
double execution_time;
double memory_usage;
};
template <typename FN, typename... T>
inline auto measure(FN fn, measurement& m, T&&... args) -> decltype(fn(std::forward<T>(args)...))
{
auto&& result = fn(std::forward<T>(args)...);
m.execution_time = 0; // example
m.memory_usage = 0;
return result;
}
相关文章:
- 如何比较两个函数的速度和性能
- 调用不在基类中的派生类函数而不进行动态强制转换,以最大程度地提高性能
- 函数局部静态变量:从性能角度来看的优点/缺点
- C++ 将函数指针与最佳性能相结合
- 为什么使用默认构造函数"{}"而不是"= default"存在性能变化?
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- 隐式转换函数的返回对象时是否会影响性能?
- 虚拟函数调用的性能作为 for 循环中的上限
- 如何提高对 std::函数侦听器的分发性能?
- 性能函数调用与乘以 1
- C++构造函数性能
- 课堂初始化(分配样式)与构造函数性能
- 类方法VS类静态函数VS简单函数-性能方面
- 对同一对象进行x调用的c++虚拟函数性能
- 由派生类调用时的虚函数性能
- c++ 11委托的函数是否比c++ 03调用init函数的函数性能差?
- 捕获Lambda函数性能的影响
- Visual c++中的函数性能计数器
- 从另一个操作符重载函数调用操作符重载函数:性能
- 内联函数性能