std::chrono::utc_clock::now

< cpp‎ | chrono‎ | utc clock

 
 
日期和时间工具
(C++11)
(C++11)
当天时刻
(C++20)



(C++20)(C++20)(C++20)(C++20)
时钟
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
日历
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
时区
(C++20)
(C++20)
(C++20)
(C++20)
C 风格日期和时间
 
 
static std::chrono::time_point<std::chrono::utc_clock> now();
(C++20 起)

返回表示当前时间的时间点。 如同以 std::chrono::utc_clock::from_sys(std::chrono::system_clock::now()) 计算结果。实现可以用更精确的 UTC 时间值。

参数

(无)

返回值

表示当前时间的时间点。

示例

#include <iostream>
#include <vector>
#include <numeric>
#include <chrono>
 
volatile int sink;
int main()
{
    for (auto size = 1ull; size < 1000000000ull; size *= 100) {
        // 记录开始时间
        auto start = std::chrono::utc_clock::now();
        // 做一些工作
        std::vector<int> v(size, 42);
        sink = std::accumulate(v.begin(), v.end(), 0u); // 确保其副效应
        // 记录结束时间
        auto end = std::chrono::utc_clock::now();
        std::chrono::duration<double> diff = end-start;
        std::cout << "Time to fill and iterate a vector of " 
                  << size << " ints : " << diff.count() << " s\n";
    }
}

可能的输出:

Time to fill and iterate a vector of 1 ints : 2.43e-07 s
Time to fill and iterate a vector of 100 ints : 4.1e-07 s
Time to fill and iterate a vector of 10000 ints : 2.519e-05 s
Time to fill and iterate a vector of 1000000 ints : 0.00207669 s
Time to fill and iterate a vector of 100000000 ints : 0.423087 s