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/

相关内容

热门资讯

黑科技有挂!德州之星辅助器怎么... 黑科技有挂!德州之星辅助器怎么用,德州辅助dto,wepoke教程(好像真的有挂)1、很好的工具软件...
aapoker有外挂(aa扑克... aapoker有外挂(aa扑克)aapoker辅助工具存在吗(辅助挂)2025新版总结(好像是真的有...
黑科技辅助(微扑克模拟器)外挂... 黑科技辅助(微扑克模拟器)外挂透视辅助app(透视)攻略教程(竟然真的是有挂)所有人都在同一条线上,...
黑科技挂!(AApoker)透... 黑科技挂!(AApoker)透明挂辅助插件,(AAPoKER)本来有挂,攻略教程(有挂攻略)科技教程...
黑科技讲解!wepoke有挂吗... 1、黑科技讲解!wepoke有挂吗网上靠谱吗,wopoker游戏辅助器,科技教程(总是真的有挂)。2...
德州之星ai软件(德州)德州w... 德州之星ai软件(德州)德州wpk辅助(辅助挂)技巧教程(果然是真的有挂)1、每一步都需要思考,不同...
黑科技好友(WepOke)黑科... 黑科技好友(WepOke)黑科技透明挂辅助技巧(透视)攻略方法(切实有挂);1、许多玩家不知道Wep...
黑科技软件!(WpK)透视辅助... 黑科技软件!(WpK)透视辅助器,(wpK)一直有挂,2025新版(有挂教程);wpK软件透明挂作为...
黑科技肯定!aapoker俱乐... 黑科技肯定!aapoker俱乐部后台可以操作吗,aapoker透视辅助,解密教程(原先有挂);支持多...
wepoke小技巧(wePok... wepoke小技巧(wePokE)wepoke挂真的(黑科技)教你教程(本来是真的有挂)1、构建自己...