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)微友辅助神器下载...
七分钟辅助!德普之星透视辅助插... 七分钟辅助!德普之星透视辅助插件,微扑克数据分析,详细教程(真的有挂)1、七分钟辅助!德普之星透视辅...
解密妙招!潮友会鱼虾蟹塞子概率... 解密妙招!潮友会鱼虾蟹塞子概率计算方式,顺欣茶楼辅助软件,详细教程(有挂解惑)1、该软件可以轻松地帮...
现场直击!wpk辅助插件,大菠... 现场直击!wpk辅助插件,大菠萝免费辅助,策略教程(有挂技术)现场直击!wpk辅助插件,大菠萝免费辅...
8分钟辅助!悟空黑桃a辅助,w... 8分钟辅助!悟空黑桃a辅助,wepoker破解器激活码,详细教程(有挂实锤)进入游戏-大厅左侧-新手...
专业方案!佛手十三道破解版安卓... 专业方案!佛手十三道破解版安卓,友友联盟辅助免费下载,详细教程(竟然有挂)1、佛手十三道破解版安卓脚...
来一盘!德普之星怎么开辅助,拱... 来一盘!德普之星怎么开辅助,拱趴大菠萝辅助工具是真的吗,技法教程(讲解有挂)1、这是跨平台的拱趴大菠...
三分钟辅助!德扑圈透视挂,we... 三分钟辅助!德扑圈透视挂,wepoker代打辅助,详细教程(有挂方略)1、玩家可以在wepoker代...
一分钟了解!wepoker透视... 一分钟了解!wepoker透视脚本网页,微信闲来辅助神器,手段教程(有挂分析)1、首先打开微信闲来辅...
详情诀窍!微信雀神挂件开挂辅助... 详情诀窍!微信雀神挂件开挂辅助,一起宁德钓蟹辅助器,详细教程(有挂方针)所有人都在同一条线上,像星星...