C++ 高精度时钟获取当前时间 std::chrono::high_resolution_clock::now
创始人
2025-01-10 03:08:14
0

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

  • 1. `std::chrono::high_resolution_clock::now`
    • 1.1. Parameters
    • 1.2. Return value
    • 1.3. Example
  • 2. `std::chrono::milliseconds`
  • 3. `std::chrono::microseconds`
    • 3.1. Example
  • References

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.

1.3. Example

#include  #include  #include  #include   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 time_span = duration_cast>(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.

#include  #include  #include  #include   int main() { 	using namespace std::chrono;  	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;  	time_point end = high_resolution_clock::now();  	duration time_span = duration_cast>(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} 10−3 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} 10−6 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  #include  #include  #include   int main() { 	std::chrono::time_point 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 end = std::chrono::high_resolution_clock::now();  	std::chrono::milliseconds time_span_ms = std::chrono::duration_cast(end - begin); 	std::chrono::microseconds time_span_us = std::chrono::duration_cast(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/

相关内容

热门资讯

透视线上!中至上饶棋牌真的有挂... 透视线上!中至上饶棋牌真的有挂透视,免费雀神挂件怎么安装(开挂)原来有挂(有挂存在)-哔哩哔哩1、每...
不少玩家反映!奇迹陕西靖边锅子... 不少玩家反映!奇迹陕西靖边锅子有没有技巧,谁有老友广东辅助器(透视)器-本来是有挂1)奇迹陕西靖边锅...
连日来!德普之星有辅助软件吗,... 连日来!德普之星有辅助软件吗,如何下载德普之星辅助软件(透视)总结脚本(果然存在有挂)-哔哩哔哩运如...
据相关数据显示!wepoker... 据相关数据显示!wepoker免费钻石,wepoker怎么发冤家牌(透视)攻略教程(切实是真的挂)1...
随着!凑一桌游戏春天辅助,朱雀... 随着!凑一桌游戏春天辅助,朱雀开心罗松开挂(透视)方法-一直是真的挂1、凑一桌游戏春天辅助脚本辅助下...
透视新版!赣南辅助是真的吗,海... 透视新版!赣南辅助是真的吗,海螺众娱辅助(开挂)一直真的是有挂(有挂透视)-哔哩哔哩1、每一步都需要...
无独有偶!hhpoker有辅助... 无独有偶!hhpoker有辅助吗,hhpoker破解工具(透视)总结脚本(其实真的是有挂)-哔哩哔哩...
最终!wepoker有脚本吗,... 最终!wepoker有脚本吗,aapoker透视怎么用(透视)举措教程(其实存在有挂)1.aapok...
网友热议!阿拉游戏免费辅助器,... 网友热议!阿拉游戏免费辅助器,浙江宝宝游戏辅助工具(透视)技巧-竟然真的是有挂1、每一步都需要思考,...
透视神器!心悦踢坑辅助器服务器... 透视神器!心悦踢坑辅助器服务器码,微乐云南小程序修改器ios(开挂)总是有挂(有挂秘诀)-哔哩哔哩1...