Std :: chrono或boost :: chrono支持CLOCK_MONOTONIC_COARSE [英] Std::chrono or boost::chrono support for CLOCK_MONOTONIC_COARSE

查看:768
本文介绍了Std :: chrono或boost :: chrono支持CLOCK_MONOTONIC_COARSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux上运行(uname说:)

Running on Linux (uname says:)

Linux 2.6.32-431.29.2.el6.x86_64 #1 SMP Sun Jul 27 15:55:46 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

我的测试显示clock_gettime时钟ID为CLOCK_MONOTONIC_COARSE的调用比使用时钟id CLOCK_MONOTONIC的调用快一个数量级。

My tests show that clock_gettime calls with a clock id of CLOCK_MONOTONIC_COARSE are an order of magnitude faster than calls that use a clock id CLOCK_MONOTONIC.

下面是测试运行的一个示例输出,在紧缩循环中调用clock_gettime 100万次,并以毫秒为单位测量耗用时间:

Here's a sample output from a test run which called clock_gettime one million times in a tight loop and measured the lapsed time in milliseconds:

CLOCK_MONOTONIC lapse 795
CLOCK_MONOTONIC_COARSE lapse 27


$ b b

这让我感到欣慰,并使分析器的结果看起来更好,但我希望我可以使用std :: chrono或boost :: chrono的可移植性和标准的一致性,而不牺牲这个速度。不幸的是,我没有找到任何方法说服chrono(任一)使用CLOCK_MONOTONIC_COARSE当它可用。我试过chrono :: steady_clock,但结果是可比的CLOCK_MONOTONIC值。

This pleases me and makes the profiler results look better, however I was hoping that I could use std::chrono or boost::chrono for portability and standard conformance without sacrificing this speed. Unfortunately I haven't found any way to convince chrono (either one) to use CLOCK_MONOTONIC_COARSE when it's available. I tried chrono::steady_clock, but the results are comparable to the CLOCK_MONOTONIC values.

有没有办法指定chrono你愿意牺牲精度的速度?

Is there a way to specify to chrono that you are willing to sacrifice precision for speed?

推荐答案

由于 Howard说它很简单,使自己的时钟 - 符合C ++ 11的类型时钟 > CLOCK_MONOTONIC_COARSE Live at Coliru ):

As Howard said it's simple make your own clock - a type conforming to the C++11 Clock requirements - that uses CLOCK_MONOTONIC_COARSE when it's available and CLOCK_MONOTONIC otherwise (Live at Coliru):

class fast_monotonic_clock {
public:
    using duration = std::chrono::nanoseconds;
    using rep = duration::rep;
    using period = duration::period;
    using time_point = std::chrono::time_point<fast_monotonic_clock>;

    static constexpr bool is_steady = true;

    static time_point now() noexcept;

    static duration get_resolution() noexcept;

private:
    static clockid_t clock_id();
    static clockid_t test_coarse_clock();
    static duration convert(const timespec&);
};

inline clockid_t fast_monotonic_clock::test_coarse_clock() {
    struct timespec t;
    if (clock_gettime(CLOCK_MONOTONIC_COARSE, &t) == 0) {
        return CLOCK_MONOTONIC_COARSE;
    } else {
        return CLOCK_MONOTONIC;
    }
}

clockid_t fast_monotonic_clock::clock_id() {
    static clockid_t the_clock = test_coarse_clock();
    return the_clock;
}

inline auto fast_monotonic_clock::convert(const timespec& t) -> duration {
    return std::chrono::seconds(t.tv_sec) + std::chrono::nanoseconds(t.tv_nsec);
}

auto fast_monotonic_clock::now() noexcept -> time_point {
    struct timespec t;
    const auto result = clock_gettime(clock_id(), &t);
    assert(result == 0);
    return time_point{convert(t)};
}

auto fast_monotonic_clock::get_resolution() noexcept -> duration {
    struct timespec t;
    const auto result = clock_getres(clock_id(), &t);
    assert(result == 0);
    return convert(t);
}

这篇关于Std :: chrono或boost :: chrono支持CLOCK_MONOTONIC_COARSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆