C++得到一个标准::时间::d的周期
C++ get period of an std::chrono::duration
我在玩std::chrono。当我做一些测试时,我想知道我是否可以获得用于构建 std::chrono::d uration 的比率,因为我想打印它。
这里有一些代码来显示我到底想做什么:
您可以通过添加 -std=c++11
标志在 Windows 和 linux (g++) 上编译它。这个小示例代码应该测量机器需要cout
到最大整数值的时间。
主.cpp
#include<iostream>
#include "stopchrono.hpp"
#include<chrono>
#include<limit>
int main (){
stopchrono<> main_timer(true);
stopchrono<unsigned long long int,std::ratio<1,1000000000>,std::chrono::high_resolution_clock> m_timer(true);//<use long long int to store ticks,(1/1000000000)sekond per tick, obtain time_point from std::chrono::high_resolution_clock>
stopchrono<unsigned long long int,std::ratio<1,1000000000>> mtimer(true);
std::cout<<"count to max of int ..."<<std::endl;
for(int i=0;i<std::numeric_limits<int>::max();i++){}
std::cout<<"finished."<<std::endl;
main_timer.stop();
m_timer.stop();
mtimer.stop();
std::cout<<std::endl<<"It took me "<<(main_timer.elapsed()).count()<<" Seconds."<<std::endl;
std::cout<<" "<<(m_timer.elapsed()).count()<<std::endl;//print amount of elapsed ticks by std::chrono::duration::count()
std::cout<<" "<<(mtimer.elapsed()).count()<<std::endl;
std::cin.ignore();
return 0;
}
停止时间.hpp
#ifndef STOPCHRONO_DEFINED
#define STOPCHRONO_DEFINED
#include<chrono>
template<class rep=double,class period=std::ratio<1>,class clock=std::chrono::steady_clock> //this templates first two parameters determines the duration type that will be returned, the third parameter defines from which clock the duration will be obtained
class stopchrono { // class for measurement of time programm parts are running
typename clock::time_point start_point;
std::chrono::duration<rep,period> elapsed_time;
bool running;
public:
stopchrono():
start_point(clock::now()),
elapsed_time(elapsed_time.zero()),
running(false)
{}
stopchrono(bool runnit)://construct already started object
running(runnit),
elapsed_time(elapsed_time.zero()),
start_point(clock::now())
{}
void start(){//set start_point to current clock::now() if not running
if(!running){
start_point=clock::now();
running=true;
}
}
void stop(){// add current duration to elapsed_time
if(running){
elapsed_time+=std::chrono::duration_cast<std::chrono::duration<rep,period>>(clock::now()-start_point);
running=false;
}
}
void reset(){// set elapsed_time to 0 and running to false
elapsed_time=elapsed_time.zero();
running=false;
}
std::chrono::duration<rep,period> elapsed(){//return elapsed_time
if(running){
return (std::chrono::duration_cast<std::chrono::duration<rep,period>>(elapsed_time+(clock::now()-start_point)));
}else{
return (elapsed_time);
}
}
bool is_running()const{// determine if the timer is running
return running;
}
};
#endif
实际样本输出
count to max of int ...
finished.
It took me 81.6503 Seconds.
81650329344
81650331344
目标样本输出
count to max of int ...
finished.
It took me 81.6503 Seconds.
81650329344 (1/1000000000)sekonds
81650331344
即使我不知道我使用了哪个时间来创建停止时间对象,如何从返回的持续时间中获取已用周期std::ratio<1,1000000000>
?这可能吗?
std::chrono::duration
类有一个 typedef period
这就是你要找的。您可以通过访问它 decltype(your_variable)::period
.应该执行以下操作
auto elapsed = main_timer.elapsed();
cout << elapsed.count() << " " << decltype(elapsed)::period::num << "/"
<< decltype(elapsed)::period::den << endl;
另请参阅此工作示例,其中打印了经过的时间和秒的比率。
相关文章:
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 标准::时间::d类型的挥发性对象
- C++20 标准::时间::d格式的缺点
- 子类化标准::时间::d
- 将标准::时间点乘以标量
- 从双秒到标准::时间:steady_clock::d的简短转换?
- 如何概括标准::时间::d?
- 将 boost:::chrono::steady_clock::time_point 转换为标准::时间::steady
- 如何将标准::时间::d转换为双倍(秒)?
- 哪个库 - 标准::时间::system_clock
- C++,你如何缩放标准::时间::d
- 如何更改标准::时间::high_resolution_clock::现在();到长
- 如何将标准::时间::high_resolution_clock::现在()转换为毫秒,微秒,..
- 如何使用谷歌测试打印标准::时间类型
- 快速转换标准::时间::time_point 到/从标准::字符串
- 如何格式化标准::时间持续时间
- 标准::时间 将天数添加到当前日期
- 如何将标准::时间::time_point转换为带有秒小数部分的日历日期时间字符串?
- 标准::时间纪元是什么时候?
- 将标准::时间与随机整数混合