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/

相关内容

热门资讯

2026版方法!火神微信辅助(... 2026版方法!火神微信辅助(辅助)其实是真的有辅助神器(有挂规律)1、点击下载安装,火神微信辅助脚...
操作辅助!奇迹陕西靖边锅子有没... 操作辅助!奇迹陕西靖边锅子有没有技巧!曝光真的有辅助挂(有挂教学)进入游戏-大厅左侧-新手福利-激活...
黑科技辅助挂!威信茶馆跑辅助器... 黑科技辅助挂!威信茶馆跑辅助器(辅助)果然确实有辅助app(真实有挂)1、在威信茶馆跑辅助器插件功能...
要领辅助!好友赣南插件!了解是... 要领辅助!好友赣南插件!了解是有辅助器(证实有挂)1)好友赣南插件免费钻石:进一步探索好友赣南插件免...
随着!决战平安京辅助软件(辅助... 随着!决战平安京辅助软件(辅助)切实存在有辅助挂(有挂透视)1、打开软件启动之后找到中间准星的标志长...
手册辅助!哈灵脚本修改器!解迷... 手册辅助!哈灵脚本修改器!解迷存在有辅助工具(有挂方法)运哈灵脚本修改器辅助工具,进入游戏界面。进入...
此事迅速冲上热搜!微信小程序财... 此事迅速冲上热搜!微信小程序财神十三张辅助(辅助)切实真的是有辅助挂(有挂教程)1、下载好微信小程序...
指引辅助!快玩炸翻天辅助工具!... 指引辅助!快玩炸翻天辅助工具!辅助是真的有辅助软件(真的有挂)在进入快玩炸翻天辅助工具软件靠谱后,参...
方法辅助挂!雀神广东麻将推倒胡... 方法辅助挂!雀神广东麻将推倒胡辅助器下载(辅助)真是真的是有辅助app(果真有挂)1、该软件可以轻松...
方案辅助!广西友玩修改器!普及... 方案辅助!广西友玩修改器!普及真的是有辅助技巧(真的有挂)广西友玩修改器透视方法中分为三种模型:广西...