2011-10-20 44 views
3

我是C++的新手,我爲C++編寫了一個用於Linux的程序。我試圖將其轉換爲Windows。我的代碼是:C++代碼從Linux到Windows的轉換

struct Timer 
{ 
    struct tms t[2]; 
    void STARTTIME (void) 
    { 
     times(t); 
    } 

    void STOPTIME(void) 
    { 
     times(t+1); 
    } 

    double USERTIME(void) 
    { 
     return ((double)((t+1)->tms_utime - t->tms_utime))/((double)sysconf(_SC_CLK_TCK)); 
    } 
}; 

對於tms_utime我發現在Visual C++項QueryPerformanceCounter,但我不能應用此。 對於sysconf(_SC_CLK_TCK)我使用CLOCKS_PER_SEC但我不知道這是多麼正確?什麼是Windows的等效代碼?

+0

你看着Boost.Timer?擁有第三方庫來幫助跨平臺開發總是一個好主意。 – Ayjay

+0

一個簡單的Windows/Linux定時器類[宋浩在這個zip](http://www.songho.ca/opengl/files/fbo.zip) – bobobobo

回答

2

下面是一個簡易替換返回了用戶的時間,而不是經過時間:

#include <windows.h> 

struct Timer 
{ 
    ULONGLONG t[2]; 

    void STARTTIME (void) 
    { 
     t[0] = getCurrentUserTime(); 
    } 

    void STOPTIME(void) 
    { 
     t[1] = getCurrentUserTime(); 
    } 

    double USERTIME(void) 
    { 
     return (t[1] - t[0])/1e7; 
    } 

private: 
    // Return current user time in units of 100ns. 
    // See http://msdn.microsoft.com/en-us/library/ms683223 
    // for documentation on GetProcessTimes() 
    ULONGLONG getCurrentUserTime() 
    { 
     FILETIME ct, et, kt, ut; 
     GetProcessTimes(GetCurrentProcess(), &ct, &et, &kt, &ut); 
     ULARGE_INTEGER t; 
     t.HighPart = ut.dwHighDateTime; 
     t.LowPart = ut.dwLowDateTime; 
     return t.QuadPart; 
    } 
}; 
2

這裏有一類我寫的,我一直使用

#ifndef HIGHPERFTIMER_H 
#define HIGHPERFTIMER_H 

#include <windows.h> 
#include <stdio.h> 

class StopWatch 
{ 
    LARGE_INTEGER freq, startTime, endTime, thisTime, lastTime ; 
    double fFreq ; 

public: 
    double total_time ; 

    StopWatch() 
    { 
    QueryPerformanceFrequency(&freq) ; 
    fFreq = (double)freq.QuadPart ; 
    total_time = 0 ; 

    printf("  --- The ffreq is %lf\n", fFreq) ; 
    } 

    void start() 
    { 
    QueryPerformanceCounter(&startTime) ; 
    thisTime = lastTime = startTime ; 
    total_time = 0.0 ; // start counter at 0 seconds 
    } 

    double stop() 
    { 
    QueryPerformanceCounter(&endTime) ; 
    total_time = (endTime.QuadPart - startTime.QuadPart)/fFreq ; 
    return total_time ; 
    } 

    void update() 
    { 
    lastTime = thisTime ; 
    QueryPerformanceCounter(&thisTime) ; 
    total_time += (thisTime.QuadPart - lastTime.QuadPart)/fFreq ; 
    } 
} ; 

#endif //HIGHPERFTIMER_H 

用法示例:

int main() 
{ 
    StopWatch stopWatch ; 
    stopWatch.start() ; 
    ///.. code.. 
    stopWatch.stop() ; 
    printf("Time elapsed: %f sec", stopWatch.total_time) ; 
} 
+0

原始代碼是測量用戶時間(即CPU時間在用戶空間中)。這可能大於或小於經過的掛鐘時間,具體取決於系統負載,「sleep()」調用等以及當前進程中的線程數。這裏的代碼測量已用時間。我不確定這種差異是否與用例有關。 – msandiford

+1

您還必須確保每次調用同一個線程的'QueryPerformance'函數,否則您將得到不同且不一致的結果。 – Daemin

0

這是(一個未經考驗的,但在邏輯上是正確的)在更換下降。 usertime函數返回第二個分辨率(作爲double),因此您需要除以所需的分辨率。

struct Timer 
{ 
    __int64 t[2]; 

    void Start() 
    { 
     QueryPerformanceCounter((LARGE_INTEGER*)&t[0]); 
    } 

    void Stop() 
    { 
     QueryPerformanceCounter((LARGE_INTEGER*)&t[1]); 
    } 

    double usertime() 
    { 
     __int64 freq; 
     QueryPerformanceFrequency((LARGE_INTEGER*)&freq); 
     return (double(t[1] - t[0]))/freq; 
    } 
}; 
+0

有趣,倒票2個月後,謹慎解釋? – Chad