当前位置:网站首页>chrono 使用备注
chrono 使用备注
2022-06-24 06:44:00 【雾散睛明】
chrono 使用备注
chrono 是一个命名空间,用于时间相关的库。
包括
Durations:时长类
Durations 的实现类有如下:
hours 时
minutes 分
seconds 秒
milliseconds 毫秒
microseconds 微秒
nanoseconds 纳秒
Time points 时间点,记录某个时刻
Clocks
将时间点与实际物理时间相关联的框架。
该库至少提供三个时钟,它们提供了将当前时间表示为时间点的方法:system_clock(系统时钟)、steady_clock(稳定时钟)和high_resolution_clock(高分辨率时钟)。
Durations 示例:
#include <iostream>
#include <ratio>
#include <chrono>
int main ()
{
typedef std::chrono::duration<int> seconds_type;
typedef std::chrono::duration<int,std::milli> milliseconds_type;
typedef std::chrono::duration<int,std::ratio<60*60>> hours_type;
hours_type h_oneday (24); // 24h
seconds_type s_oneday (60*60*24); // 86400s
milliseconds_type ms_oneday (s_oneday); // 86400000ms
seconds_type s_onehour (60*60); // 3600s
//hours_type h_onehour (s_onehour); // NOT VALID (type truncates), use:
hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour));
milliseconds_type ms_onehour (s_onehour); // 3600000ms (ok, no type truncation)
std::cout << ms_onehour.count() << "ms in 1h" << std::endl;
return 0;
}
// duration::zero
#include <iostream>
#include <chrono>
int main ()
{
using std::chrono::steady_clock;
steady_clock::time_point t1 = steady_clock::now();
std::cout << "Printing out something...\n";
steady_clock::time_point t2 = steady_clock::now();
steady_clock::duration d = t2 - t1;
if ( d == steady_clock::duration::zero() )
std::cout << "The internal clock did not tick.\n";
else
std::cout << "The internal clock advanced " << d.count() << " periods.\n";
return 0;
}
// duration::min/max
#include <iostream>
#include <chrono>
int main ()
{
std::cout << "system_clock durations can represent:\n";
std::cout << "min: " << std::chrono::system_clock::duration::min().count() << "\n";
std::cout << "max: " << std::chrono::system_clock::duration::max().count() << "\n";
return 0;
}
Clocks 示例:
// system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
//获取当前时间转换成字符串
int main ()
{
using std::chrono::system_clock;
std::chrono::duration<int,std::ratio<60*60*24> > one_day (1);
system_clock::time_point today = system_clock::now();
system_clock::time_point tomorrow = today + one_day;
std::time_t tt;
tt = system_clock::to_time_t ( today );
std::cout << "today is: " << ctime(&tt);
tt = system_clock::to_time_t ( tomorrow );
std::cout << "tomorrow will be: " << ctime(&tt);
return 0;
}
//计算时间间隔
// system_clock::from_time_t
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
int main ()
{
using namespace std::chrono;
// create tm with 1/1/2000:
std::tm timeinfo = std::tm();
timeinfo.tm_year = 100; // year: 2000
timeinfo.tm_mon = 0; // month: january
timeinfo.tm_mday = 1; // day: 1st
std::time_t tt = std::mktime (&timeinfo);
system_clock::time_point tp = system_clock::from_time_t (tt);
system_clock::duration d = system_clock::now() - tp;
// convert to number of days:
typedef duration<int,std::ratio<60*60*24>> days_type;
days_type ndays = duration_cast<days_type> (d);
// display result:
std::cout << ndays.count() << " days have passed since 1/1/2000";
std::cout << std::endl;
return 0;
}
…
duration_cast 进行时长转换
更多参考chrono
边栏推荐
- UE常用控制臺命令
- Global and Chinese market of Earl Grey tea 2022-2028: Research Report on technology, participants, trends, market size and share
- LeetCode 207:课程表(拓扑排序判断是否成环)
- Face pincher: a hot meta universe stylist
- [image fusion] multi focus and multi spectral image fusion based on pixel saliency and wavelet transform with matlab code
- buuctf misc [UTCTF2020]docx
- 阿里云全链路数据治理
- What is the mentality of spot gold worth learning from
- Shell script for MySQL real-time synchronization of binlog
- Pyhton crawls to Adu (Li Yifeng) Weibo comments
猜你喜欢

When MFC uses the console, the project path cannot have spaces or Chinese, otherwise an error will be reported. Lnk1342 fails to save the backup copy of the binary file to be edited, etc

20 not to be missed ES6 tips
![[OGeek2019]babyrop](/img/74/5f93dcee9ea5a562a7fba5c17aab76.png)
[OGeek2019]babyrop

How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode

How to open the soft keyboard in the computer, and how to open the soft keyboard in win10

get_ started_ 3dsctf_ two thousand and sixteen
![[WUSTCTF2020]alison_ likes_ jojo](/img/a9/dcc6f524772cd0b8781289cbaef63f.png)
[WUSTCTF2020]alison_ likes_ jojo

How can win11 set the CPU performance to be fully turned on? How does win11cpu set high performance mode?

Camera calibration (calibration purpose and principle)
![buuctf misc [UTCTF2020]docx](/img/e4/e160f704d6aa754e85056840e14bd2.png)
buuctf misc [UTCTF2020]docx
随机推荐
《canvas》之第4章 线条操作
What is automated testing? What software projects are suitable for automated testing?
What is the mentality of spot gold worth learning from
When MFC uses the console, the project path cannot have spaces or Chinese, otherwise an error will be reported. Lnk1342 fails to save the backup copy of the binary file to be edited, etc
2022年PMP项目管理考试敏捷知识点(1)
MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics
How to open the soft keyboard in the computer, and how to open the soft keyboard in win10
Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
6000多万铲屎官,捧得出一个国产主粮的春天吗?
[image fusion] image fusion based on directional discrete cosine transform and principal component analysis with matlab code
Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence
bjdctf_2020_babystack
Overview of C program operation mechanism
光照使用的简单总结
[learn FPGA programming from scratch -41]: vision chapter - Moore's era and Moore's law and the arrival of the post Moore Era
20 not to be missed ES6 tips
Phonics
(CVE-2020-11978)Airflow dag中的命令注入漏洞复现【vulhub靶场】
[image fusion] multi focus and multi spectral image fusion based on pixel saliency and wavelet transform with matlab code