Search code examples
c++timefpgaxilinx

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


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 checked some threads here (Testing the performance of a C++ app) and here (Timer function to provide time in nano seconds using C++), but i don't really understand how to apply it mon code ...

To make things clear : I'm not good at C++, I have never really learned the language formally but only used it several times with specific libraries. I am not even the author of the code I'm using (given to me by the professors).

My goal here is to check the time spent on each functions and globally when I execute the program on the Zedboard. The code is on Linux image on an SD card, the board booting on this image. It is using the opencv library for anImage processing application. I'm using g++ 4.6.3 as a compiler.

Thanks in advance for your answer !


Solution

  • You can create a simple timer class using the <chrono> 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<nanoseconds>(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 <chrono> 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;
    };