2016-07-13 77 views
0

我爲LinkedList編寫了一個多線程的C代碼。我正在嘗試測量代碼的吞吐量和延遲。吞吐量的測量,這裏是我的代碼測量代碼的吞吐量和延遲

clock_t begin = clock();  
    pthread_create (&t1, NULL, thread1, (void *)head); 
    pthread_create (&t2, NULL, thread2, (void *)head); 
    pthread_create (&t3, NULL, thread3, (void *)head); 
    pthread_create (&t4, NULL, thread4, (void *)head); 
    pthread_join (t1, NULL); 
    pthread_join (t2, NULL); 
    pthread_join (t3, NULL); 
    pthread_join (t4, NULL); 
clock_t end = clock(); 

而對於延遲其計算方法如下

void * thread1(void * args) 

{ 
    clock_t begin = clock(); 

/* LinkedList Operations */ 

    clock_t begin = clock(); 
} 

我是不是正確測量這兩個參數或者是有一些其他的方式來做到這一點?

在此先感謝!

+0

[複製](http://stackoverflow.com/a/5249150/5058676) – evaitl

回答

1

我個人的偏好是這樣的:

struct timespec start, end; 
clock_gettime(CLOCK_MONOTONIC_RAW, &start); 
sleep(1); 
clock_gettime(CLOCK_MONOTONIC_RAW, &end); 
const uint64_t ns = (end.tv_sec * 1000000000 + end.tv_nsec) - (start.tv_sec * 1000000000 + start.tv_nsec); 
printf("elapsed %7.02f ms (%lu ns)\n", ns/1000000.0, ns);