- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
例如,我有这样的函数可以执行一些有用的工作(对于事件驱动模拟):
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()
之外进行这些计数以使代码更清晰?
最佳答案
您可以像下面这样创建自己的装饰器:
#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);
}
关于c++ - 用装饰器让代码更清晰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458024/
你好,我是 Cone。 首先,我们思考一个问题。 为什么会有操作系统? 在我们教科书上会提到分时系统、批处理系统等等现代操作系统前的中间产物,也会讲到管理硬件的功能,但似乎没有讲到为什么有
假设我只想清除顶部的所有条目,清除 Map 的 Map 的最合适方法是什么 -关卡 map ? Map> nestedMap; 方法A:仅清除顶层 map 。 nestedMap.clear();
我时不时遇到一个问题,我不确定解决方案是什么。 我有一个 2 列布局(左边是 strit,然后是主要区域)。在主要区域,我有时会有一个次要的 2 栏布局(例如 - 对于新闻部分,那里有一个图标,然后是
我有以下代码: Created Created .clearfix:after{ clear: both; bdy: "."; display
有没有办法CLS单行输出?我不相信 CLS 有任何开关,所以也许更好的问题是: 有什么办法吗 保留所有以前的输出以供重复使用? 或 捕获当前显示的输出(就像通过标记和复制一样)? 我只是想通过实时反馈
我有一个流式布局。当布局足够宽时,一些 div(.one 和 .two)可以全部水平排列在一条线上。 当布局最终被挤压时,右侧 float 的 div (.two) 最终会出现在多行上。有没有一种方法
我面临着一个我真的不知道从哪里开始解决的问题,所以我希望这个问题不要太宽泛。 我正在制作并在屏幕上应用它,我将有一个包含一些信息的矩形(假设它是一个 )并且我需要用另一个矩形覆盖那个矩形,所以当用户
我的 UITableView 跨越 iPhone View 的大小,并有一个 tableHeaderView 保存附加内容。想要标题清晰,表格的其余部分白色,我将表格的背景颜色设置为清晰,并在 Vie
我一直在使用 ImageMagick,但它产生的结果非常模糊。 convert -density 300 ../images/favicons/procensus.svg -background tr
我有手动数据的工作项目,但我想在我的项目中添加 json 解析。我认为我需要帮助。 (必须是实时解析,如果可能的话,新的item添加时会自动释放) 我的 TableView 代码 - (void)sc
我正在制作一个响应式网站。我有 3 个 div (.block),我需要将它们水平放置在一起。 当屏幕足够宽时,这很容易实现。但是,当我使浏览器更窄时,第三个 div (3) 换行到下一行,但我想要的
如何在 Swift 3 中使这个 UITableView 和它的单元格清晰。 我已经完成了前面的线程,但我仍然得到一个白色背景。 正如您从我的代码中看到的,我已经尝试了提到的各种方法: overrid
当我使用 为了显示图标,它在谷歌浏览器中看起来非常清晰锐利。然而,当我在 Firefox 或 Internet Explorer 中打开 svg 时,图标看起来很模糊。 这些浏览器似乎将图标呈现为半像
我是一名优秀的程序员,十分优秀!