【发布时间】:2022-01-23 10:10:02
【问题描述】:
#include <iostream>
#include <chrono>
using namespace std;
class MyTimer {
private:
std::chrono::time_point<std::chrono::steady_clock> starter;
std::chrono::time_point<std::chrono::steady_clock> ender;
public:
void startCounter() {
starter = std::chrono::steady_clock::now();
}
double getCounter() {
ender = std::chrono::steady_clock::now();
return double(std::chrono::duration_cast<std::chrono::nanoseconds>(ender - starter).count()) /
1000000; // millisecond output
}
// timer need to have nanosecond precision
int64_t getCounterNs() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - starter).count();
}
};
MyTimer timer1, timer2, timerMain;
volatile int64_t dummy = 0, res1 = 0, res2 = 0;
// time run without any time measure
void func0() {
dummy++;
}
// we're trying to measure the cost of startCounter() and getCounterNs(), not "dummy++"
void func1() {
timer1.startCounter();
dummy++;
res1 += timer1.getCounterNs();
}
void func2() {
// start your counter here
dummy++;
// res2 += end your counter here
}
int main()
{
int i, ntest = 1000 * 1000 * 100;
int64_t runtime0, runtime1, runtime2;
timerMain.startCounter();
for (i=1; i<=ntest; i++) func0();
runtime0 = timerMain.getCounter();
cout << "Time0 = " << runtime0 << "ms\n";
timerMain.startCounter();
for (i=1; i<=ntest; i++) func1();
runtime1 = timerMain.getCounter();
cout << "Time1 = " << runtime1 << "ms\n";
timerMain.startCounter();
for (i=1; i<=ntest; i++) func2();
runtime2 = timerMain.getCounter();
cout << "Time2 = " << runtime2 << "ms\n";
return 0;
}
我正在尝试分析一个程序,其中某些关键部分的执行时间小于 50 纳秒。我发现我的计时器类使用std::chrono 太贵了(有计时的代码比没有计时的代码多花费 40% 的时间)。如何制作更快的计时器类?
我认为一些特定于操作系统的系统调用将是最快的解决方案。平台是 Linux Ubuntu。
编辑:所有代码都使用 -O3 编译。确保每个计时器只初始化一次,因此测量的成本仅归因于 startMeasure/stopMeasure 功能。我没有进行任何文本打印。
编辑 2: 接受的答案不包括将周期数实际转换为纳秒的方法。如果有人能做到这一点,那将非常有帮助。
【问题讨论】:
-
clock_gettime的 librt 可能很有用。 -
如果您的编译器支持内部函数,请尝试
__rdtsc? -
你如何测量untimed代码?您如何衡量 40% 的差异? 40% 是否还包括计时器本身的设置和拆卸?还是输出?
-
为什么需要测量?您是否考虑过使用分析器?它们在那里,因此您不必自己将测量代码添加到您的代码中。他们构建调用图,以便您可以准确找出瓶颈所在。考虑询问 CPU 它运行了多少个周期(尽管您仍然会遇到多线程和其他应用程序,这会给您的测量增加噪音)
-
测量不是免费的。您是否在测量过程中打印出结果?如果是这样,请删除这些。
标签: c++ linux performance optimization time