How to check time performances in a C++ program on Zedboard

前端 未结 2 2052
离开以前
离开以前 2021-01-16 14:14

I have implemented a C++ code on a Zedboard. It compiles and runs perfectly, but now i would like to check the performances in order to optimize some functions. I have chec

2条回答
  •  一生所求
    2021-01-16 14:49

    You can create a simple timer class using the header. Something like this:

    class Timer
    {
    public:
        using clock = std::chrono::steady_clock;
    
        void clear() { start(); tse = tsb; }
        void start() { tsb = clock::now(); }
        void stop()  { tse = clock::now(); }
    
        auto nsecs() const
        {
            using namespace std::chrono;
            return duration_cast(tse - tsb).count();
        }
    
        double usecs() const { return double(nsecs()) / 1000.0; }
        double msecs() const { return double(nsecs()) / 1000000.0; }
        double  secs() const { return double(nsecs()) / 1000000000.0; }
    
        friend std::ostream& operator<<(std::ostream& o, Timer const& timer)
        {
            return o << timer.secs();
        }
    
    private:
        clock::time_point tsb;
        clock::time_point tse;
    };
    

    You can use it simply like this:

    Timer timer;
    
    timer.start();
    
    // do some stuff
    std::this_thread::sleep_for(std::chrono::milliseconds(600));
    
    timer.stop();
    
    std::cout << timer << " seconds" << '\n';
    

    EDIT: On POSIX systems you can use clock_gettime() if is not available:

    class Timer
    {
    public:
        void clear() { start(); tse = tsb; }
        void start() { clock_gettime(CLOCK_MONOTONIC, &tsb); }
        void stop() { clock_gettime(CLOCK_MONOTONIC, &tse); }
    
        long nsecs() const
        {
            long b = (tsb.tv_sec * 1000000000) + tsb.tv_nsec;
            long e = (tse.tv_sec * 1000000000) + tse.tv_nsec;
            return e - b;
        }
    
        double usecs() const { return double(nsecs()) / 1000.0; }
        double msecs() const { return double(nsecs()) / 1000000.0; }
        double  secs() const { return double(nsecs()) / 1000000000.0; }
    
        friend std::ostream& operator<<(std::ostream& o, Timer const& timer)
        {
            return o << timer.secs();
        }
    
    private:
        timespec tsb;
        timespec tse;
    };
    

提交回复
热议问题