c++ - 如何在 Zedboard 上检查 C++ 程序的时间性能

标签 c++ time fpga xilinx

我已经在 Zedboard 上实现了 C++ 代码。它编译和运行完美,但现在我想检查性能以优化某些功能。 我在这里 ( Testing the performance of a C++ app ) 和这里 ( Timer function to provide time in nano seconds using C++ ) 检查了一些线程,但我真的不明白如何应用它的 mon 代码 ...

澄清一下:我不擅长 C++,我从来没有真正正式地学习过这门语言,只是在特定的库中使用过几次。我什至不是我正在使用的代码的作者(教授给我的)。

我的目标是在 Zedboard 上执行程序时检查每个函数和全局花费的时间。代码位于 SD 卡上的 Linux 镜像中,电路板在该镜像上启动。它使用图像处理应用程序的 opencv 库。我使用 g++ 4.6.3 作为编译器。

预先感谢您的回答!

最佳答案

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

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

你可以像这样简单地使用它:

Timer timer;

timer.start();

// do some stuff
std::this_thread::sleep_for(std::chrono::milliseconds(600));

timer.stop();

std::cout << timer << " seconds" << '\n';

编辑:POSIX您可以使用的系统 clock_gettime()如果<chrono>不可用:

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

关于c++ - 如何在 Zedboard 上检查 C++ 程序的时间性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433440/

相关文章:

c++ - 如何编写在 IDE 之间运行的 C++ 代码?

c++ - 如何在 C++ 中填充 glm::vec3

c++ - struct 命名空间内的 structname 的含义?

hardware - 用于特殊用途 3D 图形计算的 CUDA 或 FPGA?

c++ - 将字符转换为 ASCII 返回的数字太大

PHP 使用数据库中的 TIME 来计算小时数

ruby - Ruby 中的 Time.now 与 Time.new

sql - 在 postgres 中获取 Time from now() 函数

verilog - 如何在FPGA中永久存储数据和程序?

caching - DMA/Microblaze 直接访问用户空间页面物理地址后读取错误数据(内核分散/聚集)