【问题标题】:algorithm speed tester in C/C++C/C++中的算法速度测试器
【发布时间】:2012-07-23 08:38:16
【问题描述】:

我必须以毫秒为单位计算我的算法的速度。在 C++/C 中,我该怎么做?我需要在输入之前和输出之后写smth,但究竟是什么?

【问题讨论】:

  • 为什么需要绝对数字?您打算在不同的计算机上运行它进行比较吗?
  • Time difference in C++ 的可能重复项
  • 我对一个问题有两种不同的解决方案。因此它们具有完全相同的 Big-O 值,但在实现中它必须不同。所以我想在一台计算机和不同的时间比较它们。

标签: c++ c


【解决方案1】:

您可以使用<time.h> 中的clock() 函数
clock() 显示自您的程序启动以来经过了多少滴答。宏 CLOCKS_PER_SEC 包含每秒的滴答数,因此您实际上可以获得时间。

//We start measuring here. Remember what was the amount of ticks in the 
//beginning of the part of code you want to test:
int start = clock();
//<...>
//Do your stuff here
//<...>
int end = clock();//Now check what amount of ticks we have now. 
//To get the time, just subtract start from end, and divide by CLOCKS_PER_SEC.
std::cout << "it took " << end - start << "ticks, or " << ((float)end - start)/CLOCKS_PER_SEC << "seconds." << std::endl;

【讨论】:

  • 这可能是系统相关的,但是在Linux上,我发现clock()的粒度很粗(不准确),所以我避免优先使用它而不是其他系统函数,例如times( ) 来自
  • 不要忘记调用时钟本身会消耗一些毫秒,所以你不会得到精确的毫秒,所以你的算法花费的时间 = output_millis - invocation_clock_millis()。 invocation_clock_millis() 取决于系统。
  • 而且您还需要考虑时钟偏差,例如,如果系统已加载(通过 CPU 或通过网络),那么系统时钟可能会上升/下降 w.r.t 基本时钟。但这主要是 1 到 3 毫秒,所以不用太担心。
【解决方案2】:

没有通用的方法来测量确切的时间或滴答声。测量方法、操作系统和计算机上发生的其他事情(其他应用程序、图形输出、后台进程)都会影响结果。有多种方法可以进行“足够好”(在许多情况下)测量:

  • 库函数

    clock(...), clock_gettime(...)

来自标准库(time.h)和

gettimeofday(..) // for elapsed (wallclock) time
times(..) // process times

适用于 linux 和其他 unix 系统(sys/time.h)(编辑根据 Oleg 的评论)

  • 硬件计数器:

    __inline__ uint64_t rdtsc(void) {
      uint32_t lo, hi;
      __asm__ __volatile__( // serialize
                    "xorl %%eax,%%eax \n        cpuid":::"%rax",
                    "%rbx", "%rcx", "%rdx");
      __asm__ __volatile__("rdtsc":"=a"(lo), "=d"(hi));
      return (uint64_t) hi << 32 | lo;
    }
    
    /*...*/
    uint64_t t0 = rdtsc();
    code_to_be_tested();
    uint64_t t1 = rdtsc();
    

我更喜欢这种方法,因为它直接读取硬件计数器。

  • 对于 C++11:std:chrono::highresolution_clock

    typedef std::chrono::high_resolution_clock Clock;
      auto t0 = Clock::now();
      code_to_be_tested();
      auto t1 = Clock::now();
    

请记住,测量值不会精确到时钟周期。即纳秒。我总是将微秒(10e-6 s)计算为最小的合理时间单位。

【讨论】:

  • 即使你不使用windows,这里也是a good read about rdtsc
  • @JesseGood:感谢您的链接!很有意思。您知道 *ix 系统中有更好的方法吗?
  • gettimeofday,我假设硬件计数器会给你墙上的时间,由于操作系统多任务,这可能不是你想要的。如果你想要 Linux 上的用户时间,我建议 times() from &lt;sys/times.h&gt;
【解决方案3】:

请注意,您可以使用 C++11 chrono 库中的日期和时间实用程序。来自cppreference.com

chrono 库定义了三种主要类型(持续时间、时钟和时间点)以及实用函数和常用类型定义。

查看在 GCC 4.5.1 here 编译的文章中的示例

【讨论】:

    【解决方案4】:

    你可以使用这个函数库:

    // clock.c
    #include <time.h>
    #include "clock.h"
    
    struct clock { clock_t c1, c2; };
    
    void   start(clock *this) { this->c1 = clock(); }
    void   stop (clock *this) { this->c2 = clock(); }
    double print(clock *this) { return (double)(c1 - c2) / CLOCKS_PER_SEC; }
    
    // clock.h
    #ifndef CLOCK_H_INCLUDED
    # define CLOCK_H_INCLUDED
    
    typedef struct clock clock;
    
    extern void   start(clock *);
    extern void   stop (clock *);
    extern double print(clock *);
    
    #endif // CLOCK_H_INCLUDED
    

    但有时clock 不是很适应:你可以使用你的系统函数,这样会更准确。

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多