C++ 中的毫秒级基准测试?

发布于 2024-09-24 23:06:33 字数 164 浏览 2 评论 0原文

我并不是真的希望进行分析,因为我想对不同的简单函数进行许多不同的小型基准测试。在我的一生中,我找不到一种方法来记录 C++ 中的毫秒数,顺便说一句,我正在使用 Linux。

您能否建议以毫秒为单位获取系统时钟的方法(如果我找不到简单的方法,我可能会用秒来解决......)以及它们包含在什么标头中?

I do not really wish to profile because I was wanting to do many different small benchmarks on different simple functions. For the life of me I cannot find a way to record the amount of milliseconds in C++, I am using Linux by the way.

Can you suggest the method to get the system clock in milliseconds (I may settle with seconds if I cannot find an easy way..) and what header they are included in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-10-01 23:06:34

使用 sys/time.h 头文件中的 gettimeofday 函数,我使用此类:

#include <cstdlib>
#include <sys/time.h>

class Timer
{
    timeval timer[2];

  public:

    timeval start()
    {
        gettimeofday(&this->timer[0], NULL);
        return this->timer[0];
    }

    timeval stop()
    {
        gettimeofday(&this->timer[1], NULL);
        return this->timer[1];
    }

    int duration() const
    {
        int secs(this->timer[1].tv_sec - this->timer[0].tv_sec);
        int usecs(this->timer[1].tv_usec - this->timer[0].tv_usec);

        if(usecs < 0)
        {
            --secs;
            usecs += 1000000;
        }

        return static_cast<int>(secs * 1000 + usecs / 1000.0 + 0.5);
    }
};

例如:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    Timer tm;
    std::ostringstream ooo;
    std::string str;

    tm.start();
    for(int i = 0; i < 10000000; ++i)
    {
        ooo << "This is a string. ";
    }
    tm.stop();
    std::cout << "std::ostingstream -> " << tm.duration() << std::endl;

    tm.start();
    for(int i = 0; i < 10000000; ++i)
    {
        str += "This is a string. ";
    }
    tm.stop();
    std::cout << "std::string -> " << tm.duration() << std::endl;
}

using gettimeofday function from sys/time.h header file, i use this class:

#include <cstdlib>
#include <sys/time.h>

class Timer
{
    timeval timer[2];

  public:

    timeval start()
    {
        gettimeofday(&this->timer[0], NULL);
        return this->timer[0];
    }

    timeval stop()
    {
        gettimeofday(&this->timer[1], NULL);
        return this->timer[1];
    }

    int duration() const
    {
        int secs(this->timer[1].tv_sec - this->timer[0].tv_sec);
        int usecs(this->timer[1].tv_usec - this->timer[0].tv_usec);

        if(usecs < 0)
        {
            --secs;
            usecs += 1000000;
        }

        return static_cast<int>(secs * 1000 + usecs / 1000.0 + 0.5);
    }
};

for example:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    Timer tm;
    std::ostringstream ooo;
    std::string str;

    tm.start();
    for(int i = 0; i < 10000000; ++i)
    {
        ooo << "This is a string. ";
    }
    tm.stop();
    std::cout << "std::ostingstream -> " << tm.duration() << std::endl;

    tm.start();
    for(int i = 0; i < 10000000; ++i)
    {
        str += "This is a string. ";
    }
    tm.stop();
    std::cout << "std::string -> " << tm.duration() << std::endl;
}
请持续率性 2024-10-01 23:06:34

如果您使用的是 x86 CPU,则可以使用 rdtsc 汇编指令 http://en.wikipedia.org/wiki/ Rdtsc 以便获取两个(或多个)命令执行之间的 CPU 时钟数。
但:
1. 所有 rdtsc 命令应在同一 CPU 核心上运行(如果您有多核 CPU)。
2. CPU 应以恒定时钟频率运行(应禁用 CPU 电源管理)。

迪马

If you are using x86 CPU, you can use rdtsc assembler instructions http://en.wikipedia.org/wiki/Rdtsc in order to get number of CPU clocks between execution of two (or more) commands.
But:
1. All rdtsc commands should run on same CPU core (in case if you have multi core CPU).
2. CPU should run in constant clock frequency (CPU power management should be disabled).

Dima

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文