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/

相关内容

热门资讯

2024新版教程!wpk辅助透... 2024新版教程!wpk辅助透视(德扑数据软件)其实真的有挂(详细透明挂教程)1、德扑数据软件系统规...
AI辅助!德扑之星刷数据(透视... AI辅助!德扑之星刷数据(透视)外挂透明挂辅助器安装(有挂存在)-哔哩哔哩1、让任何用户在无需AI插...
教你攻略!德州ai辅助器(德扑... 教你攻略!德州ai辅助器(德扑ai助手)其实真的是有挂(详细透视辅助教程)教你攻略!德州ai辅助器(...
教你攻略辅助!德扑数据分析软件... 教你攻略辅助!德扑数据分析软件(透明)外挂透明挂辅助app(有挂工具)-哔哩哔哩1、德扑数据分析软件...
wpk教程!wpk微扑克有辅助... wpk教程!wpk微扑克有辅助吗(wepoke模拟器)原来确实真的有挂(详细透视辅助教程)1、这是跨...
2024新版总结辅助!德州辅助... 2024新版总结辅助!德州辅助软件线上(透视)外挂透明挂辅助插件(真的有挂)-哔哩哔哩1、下载好德州...
透明挂教程!wepoke软件收... 透明挂教程!wepoke软件收费是真的吗(德州之星辅助)原来真的是有挂(详细辅助挂教程)1)wepo...
2024新版技巧辅助!wepo... 2024新版技巧辅助!wepoke游戏数据有说法吗(透视)外挂透明挂辅助器安装(有挂详情)-哔哩哔哩...
技巧教程!pokerworld... 技巧教程!pokerworld下载(WPK透视辅助)原来真的有挂(详细透明挂教程);1分钟了解详细教...
德州论坛辅助!wpk外挂被实锤... 德州论坛辅助!wpk外挂被实锤(透明)外挂透明挂辅助app(有挂分析)-哔哩哔哩是一款可以让一直输的...