在C ++中聚合代码块的挂起时间

时间:2018-03-21 22:38:44

标签: c++ profiling timing boost-timer

我有一个很大的代码库,我想手动添加一些定时器来分析代码的某些部分。 其中一些部分属于循环,所以我想聚合每次迭代所花费的所有时间。

我想在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,创建一个计时器地图,然后在每次迭代时恢复和停止?

1 个答案:

答案 0 :(得分:1)

不是很先进,但对于基本时间测量,您可以使用std::chrono库,特别是std::chrono::high_resolution_clock - 时钟 实施提供的最小滴答期(=最高准确度)。

对于一些更简单的时间测量,我使用了与此类似的RAII类:

#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;
    }
};

当然这是一个非常简单的课程,在用于实际测量之前需要进行一些改进。

您可以使用此类,如:

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

对于简单的时间测量案例,这可能比使用更容易 一个完整的计时器库,它只是几行代码,你没有 需要包含大量标题。