如何在Zedboard上的C ++程序中检查时间性能 [英] How to check time performances in a C++ program on Zedboard

查看:56
本文介绍了如何在Zedboard上的C ++程序中检查时间性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Zedboard上实现了C ++代码.它可以编译并完美运行,但是现在我想检查性能以优化某些功能. 我已经在此处检查了一些线程(测试C ++应用的性能),并且此处(使用C ++的计时器功能以纳秒为单位提供时间),但我不太了解如何在代码中应用它……

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 ...

弄清楚点:我不擅长C ++,我从未真正地正式学习过该语言,只在特定的库中使用过几次.我什至不是我正在使用的代码的作者(教授给我的).

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).

我的目标是检查在Zedboard上执行程序时在每个功能上花费的时间以及全局检查时间.该代码在SD卡上的Linux映像上,板在该映像上启动.它正在将opencv库用于图像处理应用程序.我正在使用g ++ 4.6.3作为编译器.

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.

预先感谢您的回答!

推荐答案

您可以使用<chrono>标头创建一个简单的计时器类.像这样:

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

:在POSIX系统上,如果<chrono>不可用,则可以使用clock_gettime():

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

这篇关于如何在Zedboard上的C ++程序中检查时间性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆