2012-07-23 78 views
1

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

+0

为什么你需要绝对数字?你打算在不同的计算机上运行它来进行比较吗? – 2012-07-23 08:39:31

+0

可能重复的[时间差异在C++](http://stackoverflow.com/questions/307596/time-difference-in-c) – jogojapan 2012-07-23 08:39:35

+1

我有两个不同的问题的解决方案。所以它们具有完全相同的Big-O值,但在实施中它必须不同。所以我想在一台电脑和不同的时间比较它们。 – 2012-07-23 08:43:37

回答

9

你可以使用clock()功能从<time.h>
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; 
+2

这可能与系统有关,但在Linux上,我发现clock()是非常粗糙的(不准确),所以我避免使用它优先于其他系统函数,比如来自 Oleg2718281828 2012-07-23 09:01:55

+0

的时间()不要忘记时钟本身的调用会消耗一些毫秒,所以你不会得到精确的毫秒,所以你的算法花费时间= output_millis - invocation_clock_millis()。 invocation_clock_millis()是依赖于系统的。 – Mohan 2012-07-23 09:57:55

+0

而且你还需要采取时钟偏移,例如,如果系统加载(通过CPU或网络),那么系统时钟可能上升/下降w.r.t基时钟。但是这大部分是1到3毫秒,所以不用太担心。 – Mohan 2012-07-23 09:59:08

1

有没有一般的方法来衡量准确的时间或滴答声。计算机上的测量方法,操作系统和其他事情(其他应用程序,图形输出,后台进程)将影响结果。有不同的方法来做到 “足够好”(在许多情况下)的测量:

  • 库函数

    时钟(...),clock_gettime(...)

从标准库(在time.h)和

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

用于Linux和其他UNIX系统(在sys/time.h)(根据奥列格的评论)

  • 硬件计数器编辑:

    __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(); 
    

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

+0

即使你不使用窗口,这里是[关于rdtsc的一个很好的阅读](http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693%28v=vs.85%29.aspx )。 – 2012-07-23 09:10:47

+0

@JesseGood:感谢您的链接!很有意思。你知道在* ix系统中有更好的方法吗? – steffen 2012-07-23 09:17:02

+0

'gettimeofday',并且我假设硬件计数器会给你一段时间,因为操作系统的多任务可能不是你想要的。如果你想在Linux上的用户时间,我建议'从'' – Oleg2718281828 2012-07-23 09:17:27

0

您可以使用此函数库:

// 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不是很适应:你可以用你的系统的功能,它可以的更精确。

1

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

计时库定义了三种主要类型(持续时间,时钟和时间点)以及实用功能和常见typedef。

请参阅GCC 4.5中编译的文章中的示例。1 here