C++ 高精度时钟获取当前时间 std::chrono::high_resolution_clock::now

1. std::chrono::high_resolution_clock::now

Get current time
https://cplusplus.com/reference/chrono/high_resolution_clock/now/

// public static member function
static time_point now() noexcept;

Returns the current time_point in the frame of the high_resolution_clock.

1.1. Parameters

none

1.2. Return value

The time_point representing the current time.

time_point is a member type, defined as an alias of time_point<high_resolution_clock>.

1.3. Example

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main() {
	using namespace std::chrono;

	high_resolution_clock::time_point begin = high_resolution_clock::now();

	std::cout << "Printing out 1000 stars...\n";
	for (int i = 0; i < 1000; ++i) { std::cout << "*"; }
	std::cout << std::endl;

	high_resolution_clock::time_point end = high_resolution_clock::now();

	duration<double> time_span = duration_cast<duration<double>>(end - begin);

	std::cout << "It took me " << time_span.count() << " seconds.";
	std::cout << std::endl;

	return 0;
}

在这里插入图片描述

Printing out 1000 stars...
****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
It took me 0.0411305 seconds.
请按任意键继续. . .

time_point is a member type, defined as an alias of time_point<high_resolution_clock>.

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main() {
	using namespace std::chrono;

	time_point<high_resolution_clock> begin = high_resolution_clock::now();

	std::cout << "Printing out 1000 stars...\n";
	for (int i = 0; i < 1000; ++i) { std::cout << "*"; }
	std::cout << std::endl;

	time_point<high_resolution_clock> end = high_resolution_clock::now();

	duration<double> time_span = duration_cast<duration<double>>(end - begin);

	std::cout << "It took me " << time_span.count() << " seconds.";
	std::cout << std::endl;

	return 0;
}
Printing out 1000 stars...
****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
It took me 0.033101 seconds.
请按任意键继续. . .

2. std::chrono::milliseconds

Duration in milliseconds

// class
typedef duration < /* see rep below */, milli > milliseconds;

Instantiation of duration to represent milliseconds.

A millisecond (from milli- and second; symbol: ms) is a unit of time in the International System of Units equal to one thousandth (0.001 or 1 0 − 3 10^{-3} 103 or 1/1000) of a second or 1000 microseconds.

1 millisecond (1 ms)

3. std::chrono::microseconds

Duration in microseconds

// class
typedef duration < /* see rep below */, micro > microseconds;

Instantiation of duration to represent microseconds.

A microsecond is a unit of time in the International System of Units (SI) equal to one millionth (0.000001 or 1 0 − 6 10^{-6} 106 or 1/1,000,000) of a second. Its symbol is μs, sometimes simplified to us when Unicode is not available.

1 microsecond (1 μs)

3.1. Example

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main() {
	std::chrono::time_point<std::chrono::high_resolution_clock> begin = std::chrono::high_resolution_clock::now();

	std::cout << "Printing out 1000 stars...\n";
	for (int i = 0; i < 1000; ++i) { std::cout << "*"; }
	std::cout << std::endl;

	std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();

	std::chrono::milliseconds time_span_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
	std::chrono::microseconds time_span_us = std::chrono::duration_cast<std::chrono::microseconds>(end - begin);

	std::cout << "It took me " << time_span_ms.count() << " milliseconds (ms).\n";
	std::cout << "It took me " << time_span_us.count() << " microseconds (us).\n";
	std::cout << std::endl;

	return 0;
}

在这里插入图片描述

Printing out 1000 stars...
****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
It took me 33 milliseconds (ms).
It took me 33908 microseconds (us).

请按任意键继续. . .

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值