C++获取std::chrono::duration的周期

3
我正在尝试使用std :: chrono进行一些测试。在测试过程中,我想知道构建std :: chrono :: duration所使用的比率,因为我想打印它。
以下是一些代码,以显示我要做的确切内容:
您应该能够通过添加-std = c ++ 11标志在Windows和Linux(g ++)上编译此小示例代码,该代码应测量机器需要将cout输出到最大int值所需的时间。
主文件main.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;
}

stopchrono.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> 呢?即使我不知道用于创建 stopchrono 对象的比率是什么? 这是否可能?
1个回答

4
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;

另请参阅这个工作示例,它会打印出经过的时间和秒比率。


@RobertHensing 这里有一个更新的示例:[http://coliru.stacked-crooked.com/a/1ff7c26bdaf960d1]。 - Mehrwolf
这里有一个带有花哨字符串化程序的示例(我为自己编写的,并且认为分享出来会很好)。 - Guss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接