2011-10-06 40 views
0

我如何計算在下面的代碼執行時間:如何計算C中的執行時間?

#include <stdio.h> /* Core input/output operations       */ 
#include <stdlib.h> /* Conversions, random numbers, memory allocation, etc. */ 
#include <math.h> /* Common mathematical functions      */ 
#include <time.h> /* Converting between various date/time formats   */ 
#include <sys/time.h> 
#define PI  3.1415926535 /* Known vaue of PI       */ 
#define NDARTS  128 /* Number of darts thrown     */ 
double pseudo_random(double a, double b) { 
double r; /* Random number */ 
r = ((b - a) * ((double) rand()/(double) RAND_MAX)) + a; 
return r; 
} 
int main (int argc, char *argv[]) { 
int n_procs,  /* Number of processors     */ 
llimit,  /* Lower limit for random numbers  */ 
ulimit,  /* Upper limit for random numbers  */ 
n_circle,  /* Number of darts that hit the circle */ 
i;    /* Dummy/Running index     */ 
double pi_sum,  /* Sum of PI values from each WORKER */ 
x,    /* x coordinate, betwen -1 & +1   */ 
y,    /* y coordinate, betwen -1 & +1   */ 
z,    /* Sum of x^2 and y^2     */ 
error;   /* Error in calculation of PI   */ 
clock_t start_time, /* Wall clock - start time    */ 
end_time;  /* Wall clock - end time    */ 
struct timeval stime, starttime1, endtime1; 
struct timeval tv1, tv2, diff; 

llimit = -1; 
ulimit = 1; 
n_circle = 0; 
printf("\n Monte Carlo method of finding PI\n\n"); 
printf(" Number of processors : %d\n", n_procs); 
printf(" Number of darts  : %d\n\n", NDARTS); 
gettimeofday(&tv1, NULL); 
gettimeofday(&stime, NULL); 
srand(stime.tv_usec * stime.tv_usec * stime.tv_usec * stime.tv_usec); 
for (i = 1; i <= NDARTS; i++) { 
x = pseudo_random(llimit, ulimit); 
y = pseudo_random(llimit, ulimit); 
z = pow(x, 2) + pow(y, 2); 
if (z <= 1.0) { 
    n_circle++; 
    } 
} 


pi_sum = 4.0 * (double)n_circle/(double)NDARTS; 
pi_sum = pi_sum/n_procs; 
error = fabs((pi_sum - PI)/PI) * 100; 
gettimeofday(&tv2, NULL); 
double timeval_subtract (result, x, y) 
{ 
result = ((double) x - (double) y)/(double)CLOCKS_PER_SEC; 
} 
double result1 = timeval_subtract(&diff, &tv1, &tv2); 
printf(" Known value of PI : %11.10f\n", PI); 
printf(" Average value of PI : %11.10f\n", pi_sum); 
printf(" Percentage Error  : %10.8f\n", error); 
printf(" Time : \n", clock()); 
printf(" Start Time : \n",&tv1); 
printf(" End Time :\n", &tv2); 
printf(" Time elapsed (sec) : \n", result1); 
return 0; 
} 

我用timeval_subtract功能,當我執行的代碼,我得到:

Monte Carlo method of finding PI 

Number of processors : 16372 
Number of darts  : 128 

Known value of PI : 3.1415926535 
Average value of PI : 0.0002004184 
Percentage Error  : 99.99362048 
Time : 
Start Time : 
End Time : 
Time elapsed (sec) : 

首先,我找不到找到處理器數量的錯誤(我必須得到1個處理器)。

第二個「哪個是最重要的一點」,爲什麼我的時間,開始時間,結束時間和時間都是空的?

+0

我想你可能要考慮使用一個分析器。 – stdcall

+1

請打開你的編譯器的警告。它會告訴你打印輸出和'n_procs'有什麼問題。 – Mat

+0

clock()不會測量程序執行的時間,但會嘗試返回CPU使用的時鐘週期(通常在tics或jiffies中測量)。但即使您將此值轉換爲秒(通過將其除以CLOCKS_PER_SEC,結果可能會與程序實際完成時間有很大的不同,它的差異程度取決於程序的功能。 – alk

回答

1

因爲你沒有爲他們足夠的格式字符串,你需要的東西開始用「%」,如:

printf(" Time :%d \n", clock()); 
1

n_procs從未初始化,即會打印在16372-值恰好成爲之前在堆棧中的東西。

C標準庫不提供查詢處理器數量或高性能計時器的功能,因此您必須查看其他方法來查詢此問題。例如,POSIX和Windows API都提供了這樣的功能。

編輯:請參閱Programmatically find the number of cores on a machine瞭解如何初始化n_procs。看你如何使用gettimeofday,你可能在一些unix變種; 「n_procs = sysconf(_SC_NPROCESSORS_ONLN);」可能是你想要的。

1

試試這個:

printf(" Time : %lu\n", clock()); 
printf(" Start Time : %lds %ldus\n", tv1.tv_sec, tv1.tv_usec); 
printf(" End Time : %lds %ldus\n", tv2.tv_sec, tv2.tv_usec); 

而且爲:

double timeval_subtract (result, x, y) 

使用以下方法來返回微秒的時間差:

long timeval_subtract (struct timeval * result, struct timeval * x, struct timeval * y) 
{ 
    long usec = x->tv_sec * 1000000L + x->tv_usec; 
    usec -= (y->tv_sec * 1000000L + y->tv_usec); 

    result->tv_sec = usec/1000000L; 
    result->tv_usec = usec % 1000000L; 

    return usec; 
} 

根據兩者的區別日期xy函數返回值timeval_subtract(不是由result!表示的值)可能是錯誤的,因爲溢出。

假設一個長是32位寬該溢流將差異出現超過4294s時,很長一段具有64位(其應該是一個64位計算機的情況下)對子級更晚後發生溢出... ;-)

+0

我得到了這個錯誤 錯誤:期望')'在'*'標記之前: long timeval_subtract(timeval * result,timeval * x,timeval * y) –

+0

@NUM ONE:Sry,剛剛添加了缺少的'struct 。 – alk

+0

@NUM ONE:通過更好的解決方案替換示例 – alk

0

我會嘗試以下方法:

int  timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { 

    if (x->tv_usec < y->tv_usec) { 
      int nsec = (y->tv_usec - x->tv_usec)/1000000 + 1; 
      y->tv_usec -= 1000000 * nsec; 
      y->tv_sec += nsec; 
    } 
    if (x->tv_usec - y->tv_usec > 1000000) { 
      int nsec = (x->tv_usec - y->tv_usec)/1000000; 
      y->tv_usec += 1000000 * nsec; 
      y->tv_sec -= nsec; 
    } 

    result->tv_sec = x->tv_sec - y->tv_sec; 
    result->tv_usec = x->tv_usec - y->tv_usec; 

    return x->tv_sec < y->tv_sec; 
} 

void Start (struct timeval *timer_profiling) { 
     if (timer_profiling == NULL) return; 
     gettimeofday (timer_profiling , NULL); 
     return; 
} 

void End (struct timeval *timer_profiling , char *msg) { 
     struct timeval res; 
     struct timeval now; 
     gettimeofday (&now , NULL); 

     if (msg == NULL)  return; 

     timeval_subtract (&res , &now , timer_profiling); 
     sprintf (msg , "[ %ld,%.3ld ms]" , res.tv_sec*1000 + (long)round(res.tv_usec/1000) , res.tv_usec - (long)round(res.tv_usec/1000)*1000); 

     return; 
} 

開始(& S)一個與分配timer_profiling,然後通過調用結束(& S,BUFF)檢索結果中的字符串;

+0

OP源中提供的timeval_subtract函數不起作用。 – alk

+0

哦,是的,你是對的,添加了一個替代實現。感謝您指出! – SCO

+0

並且它工作...? – alk