2012-07-23 76 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