例如,我有这样的功能,它执行一些有用的工作(用于事件驱动的模拟):
int function()
{
do_useful_work();
return 0;
}
如果我需要测量此useful_work
的性能,我应该这样做:
int function()
{
count_time(time_before);
count_X_metrics(X_before);
do_useful_work();
count_time(time_after);
count_X_metrics(X_after);
return 0;
}
这种方法使代码更加笨拙。有没有办法,模式在int function()
之外进行这些计数以使代码更清晰?
答案 0 :(得分:4)
您可以创建自己的装饰器,如下所示:
#include<functional>
#include <iostream>
void count_time() {};
void count_X_metrics() {};
void decorator(std::function<void()> work)
{
count_time();
count_X_metrics();
work();
count_time();
count_X_metrics();
}
void do_work_1() {
std::cout << "Hello, World 1!" << std::endl;
}
void do_work_2() {
std::cout << "Hello, World 2!" << std::endl;
}
int main() {
decorator(do_work_1);
decorator(do_work_2);
}
编辑:我不确定你的count_time
和count_X_metrics
函数是如何工作的,但是如果你需要更复杂的东西或者跟踪状态的方法,你可以创建一个对象这样做对你有用。这肯定不同于你的需要,但希望它传达了我想要的观点:
#include<functional>
#include <iostream>
int current_time() { return 0; }
int x_metric() { return 0; }
class Timer {
public:
void time(std::function<void()> work) {
// Capture state before
int starttime = current_time();
int startmetric = x_metric();
work();
// Capture state after
int endtime = current_time();
int endmetric = x_metric();
// Update results
ellapsed = endtime - starttime;
metric = endmetric - startmetric;
// Possibly do something with the metrics here.
// ...
}
int get_ellapsed() { return ellapsed; }
int get_metric() { return metric; }
private:
int ellapsed;
int metric;
};
void do_work_1() {
std::cout << "Hello, World 1!" << std::endl;
}
void do_work_2() {
std::cout << "Hello, World 2!" << std::endl;
}
int main() {
Timer t;
t.time(do_work_1);
// Possibly do something with the metrics here.
// cout << t.get_ellapsed();
t.time(do_work_2);
}