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/

相关内容

热门资讯

一分钟了解开挂!友空互娱开挂多... 一分钟了解开挂!友空互娱开挂多功能AI方法,友空互娱真是是有开挂方法1、友空互娱脚本辅助下载、友空互...
我来教大家透视挂!葫芦娃大厅透... 我来教大家透视挂!葫芦娃大厅透视挂多功能AIapp,葫芦娃大厅其实是真的有透视挂app1、超多福利:...
玩家必看科普开挂!福城娱乐开挂... 玩家必看科普开挂!福城娱乐开挂多功能AI插件,福城娱乐切实真的是有开挂插件1、打开软件启动之后找到中...
玩家必看科普外挂!海螺互娱外挂... 玩家必看科普外挂!海螺互娱外挂多功能AI方法,海螺互娱其实是真的有外挂方法1、实时海螺互娱透视辅助更...
盘点十款开挂!开心娱乐开挂多功... 盘点十款开挂!开心娱乐开挂多功能AI工具,开心娱乐一直真的有开挂工具1、盘点十款开挂!开心娱乐开挂多...
发现玩家外挂!神牛大厅外挂多功... 发现玩家外挂!神牛大厅外挂多功能AI软件,神牛大厅切实真的是有外挂软件1、玩家可以在神牛大厅透视最简...
技巧知识分享外挂!西兵外挂多功... 技巧知识分享外挂!西兵外挂多功能AI攻略,西兵果然真的是有外挂攻略西兵透视方法中分为三种模型:西兵靠...
实测交流作弊器!七天娱乐作弊器... 实测交流作弊器!七天娱乐作弊器多功能AIapp,七天娱乐总是真的有作弊器app进入游戏-大厅左侧-新...
一分钟揭秘外挂!欢盈娱乐外挂多... 一分钟揭秘外挂!欢盈娱乐外挂多功能AI软件,欢盈娱乐确实真的有外挂软件小薇(辅助器软件下载)致您一封...
科技通报透视挂!豪发娱乐透视挂... 科技通报透视挂!豪发娱乐透视挂多功能AI插件,豪发娱乐总是真的是有透视挂插件1、每一步都需要思考,不...