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/

相关内容

热门资讯

透视黑科技!wepoker钻石... 透视黑科技!wepoker钻石怎么看底牌(透视)从来有挂(详细辅助系统教程);1分钟了解详细教程(微...
透视脚本!wepoker免费永... 透视脚本!wepoker免费永久脚本(透视)详细辅助透明挂教程(一直真的有挂)1、在wepoker免...
透视科技!aapoker透视怎... 透视科技!aapoker透视怎么用,德普软件(详细辅助必胜教程)aapoker透视怎么用软件透明挂微...
透视规律(wePOKE)wep... 透视规律(wePOKE)wepower让系统发好牌(透视)详细辅助分享教程关于wepower让系统发...
透视最新!aapoker能控制... 1、透视最新!aapoker能控制牌吗(透视)原来有挂(详细辅助大神讲解);详细教程。2、aapok...
透视辅助!aapoker辅助是... 透视辅助!aapoker辅助是真的吗,werplan怎么透视(详细辅助介绍教程)1、下载好aapok...
透视实锤!wpk辅助器安装(透... 透视实锤!wpk辅助器安装(透视)详细辅助微扑克教程(一直有挂)1、全新机制【wpk辅助器安装软件透...
透视透视(WePoKer)wp... 透视透视(WePoKer)wpk发牌算法(透视)详细辅助介绍教程;支持2-10人实时对战,虚拟庄家发...
透视工具!拱趴大菠萝辅助神器(... 透视工具!拱趴大菠萝辅助神器(透视)果然真的有挂(详细辅助细节方法);支持2-10人实时对战,虚拟庄...
透视透视!智星德州菠萝插件官网... 透视透视!智星德州菠萝插件官网,智星德州插件2024最新版(详细辅助黑科技教程);1、实时智星德州菠...