C++ 中代码块的聚合墙时间
Aggregate wall time of code blocks in C++
我的代码库很大,我想手动添加一些计时器来分析代码的某些部分。
其中一些部分在一个循环中,所以我想汇总每次迭代在那里花费的所有时间。
我想用 Pythonic 伪代码做什么:
time_step_1 = 0
time_step_2 = 0
for pair in pairs:
start_step_1 = time.now()
run_step_1(pair)
time_step_1 += start_step_1 - time.now()
start_step_2 = time.now()
run_step_2(pair)
time_step_2 += start_step_2 - time.now()
print("Time spent in step 1", time_step_1)
print("Time spent in step 2", time_step_2)
C++ 中是否有库可以执行此操作?
否则你会推荐使用 boost::timer
,创建一个计时器映射,然后在每次迭代时恢复和停止吗?
不是很高级,但对于基本的时间测量,您可以使用 std::chrono
库,特别是 std::chrono::high_resolution_clock
- 时钟
具有实现提供的最小滴答周期(=最高精度)。
对于一些更琐碎的时间测量,我使用了类似于此的 RAII classes:
#include <chrono>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
class TimeMeasureGuard {
public:
using clock_type = std::chrono::high_resolution_clock;
private:
const std::string m_testName;
std::ostream& m_os;
clock_type::time_point started_at;
clock_type::time_point ended_at;
public:
TimeMeasureGuard(const std::string& testName, std::ostream& os = std::cerr)
: m_testName(testName), m_os(os)
{
started_at = clock_type::now();
}
~TimeMeasureGuard()
{
ended_at = clock_type::now();
// Get duration
const auto duration = ended_at - started_at;
// Get duration in nanoseconds
const auto durationNs = std::chrono::nanoseconds(duration).count();
// ...or in microseconds:
const auto durationUs
= std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
// Report total run time into 'm_os' stream
m_os << "[Test " << std::quoted(m_testName) << "]: Total run time: "
<< durationNs << " ns, " << "or: " << durationUs << " us" << std::endl;
}
};
当然这是一个非常简单的class,在用于实际测量之前需要进行一些改进。
您可以像这样使用 class:
std::uint64_t computeSquares()
{
std::uint64_t interestingNumbers = 0;
{
auto time_measurement = TimeMeasureGuard("Test1");
for (std::uint64_t x = 0; x < 1'000; ++x) {
for (std::uint64_t y = 0; y < 1'000; ++y) {
if ((x * y) % 42 == 0)
++interestingNumbers;
}
}
}
return interestingNumbers;
}
int main()
{
std::cout << "Computing all x * y, where 'x' and 'y' are from 1 to 1'000..."
<< std::endl;
const auto res = computeSquares();
std::cerr << "Interesting numbers found: " << res << std::endl;
return 0;
}
输出为:
Computing all x * y, where 'x' and 'y' are from 1 to 1'000...
[Test "Test1"]: Total run time: 6311371 ns, or: 6311 us
Interesting numbers found: 111170
对于简单的时间测量情况,这可能比使用
一个完整的计时器库,它只是几行代码,你不需要
需要包括很多 headers.
我的代码库很大,我想手动添加一些计时器来分析代码的某些部分。 其中一些部分在一个循环中,所以我想汇总每次迭代在那里花费的所有时间。
我想用 Pythonic 伪代码做什么:
time_step_1 = 0
time_step_2 = 0
for pair in pairs:
start_step_1 = time.now()
run_step_1(pair)
time_step_1 += start_step_1 - time.now()
start_step_2 = time.now()
run_step_2(pair)
time_step_2 += start_step_2 - time.now()
print("Time spent in step 1", time_step_1)
print("Time spent in step 2", time_step_2)
C++ 中是否有库可以执行此操作?
否则你会推荐使用 boost::timer
,创建一个计时器映射,然后在每次迭代时恢复和停止吗?
不是很高级,但对于基本的时间测量,您可以使用 std::chrono
库,特别是 std::chrono::high_resolution_clock
- 时钟
具有实现提供的最小滴答周期(=最高精度)。
对于一些更琐碎的时间测量,我使用了类似于此的 RAII classes:
#include <chrono>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
class TimeMeasureGuard {
public:
using clock_type = std::chrono::high_resolution_clock;
private:
const std::string m_testName;
std::ostream& m_os;
clock_type::time_point started_at;
clock_type::time_point ended_at;
public:
TimeMeasureGuard(const std::string& testName, std::ostream& os = std::cerr)
: m_testName(testName), m_os(os)
{
started_at = clock_type::now();
}
~TimeMeasureGuard()
{
ended_at = clock_type::now();
// Get duration
const auto duration = ended_at - started_at;
// Get duration in nanoseconds
const auto durationNs = std::chrono::nanoseconds(duration).count();
// ...or in microseconds:
const auto durationUs
= std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
// Report total run time into 'm_os' stream
m_os << "[Test " << std::quoted(m_testName) << "]: Total run time: "
<< durationNs << " ns, " << "or: " << durationUs << " us" << std::endl;
}
};
当然这是一个非常简单的class,在用于实际测量之前需要进行一些改进。
您可以像这样使用 class:
std::uint64_t computeSquares()
{
std::uint64_t interestingNumbers = 0;
{
auto time_measurement = TimeMeasureGuard("Test1");
for (std::uint64_t x = 0; x < 1'000; ++x) {
for (std::uint64_t y = 0; y < 1'000; ++y) {
if ((x * y) % 42 == 0)
++interestingNumbers;
}
}
}
return interestingNumbers;
}
int main()
{
std::cout << "Computing all x * y, where 'x' and 'y' are from 1 to 1'000..."
<< std::endl;
const auto res = computeSquares();
std::cerr << "Interesting numbers found: " << res << std::endl;
return 0;
}
输出为:
Computing all x * y, where 'x' and 'y' are from 1 to 1'000...
[Test "Test1"]: Total run time: 6311371 ns, or: 6311 us
Interesting numbers found: 111170
对于简单的时间测量情况,这可能比使用 一个完整的计时器库,它只是几行代码,你不需要 需要包括很多 headers.