我正在玩 std::chrono 。当我做一些测试时,我想知道是否可以获得用于构造 std::chrono::duration 的比率,因为我想打印它。
这里有一些代码来显示我到底想要做什么:
您应该能够通过添加-std=c++11标志在 Windows 和 Linux (g++) 上编译它。这个小示例代码应该测量您的机器达到最大 int 值所需的时间cout。
主程序
#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 …Run Code Online (Sandbox Code Playgroud)