- 论坛徽章:
- 0
|
实在是想不通,一个线程的时候打印出的时间是对的,一旦多线程那么时间就会变为负的,而且绝对值也不对。各位大神帮忙想一下,问题出在哪?
另外还想问一下,linux下如何得到一个程序的执行时间?time命令还是在程序中用clock()?
贴一下我大体的代码:
int main(int argc ,char **argv){
clock_t start,end;
double duration;
start =clock();
...中间略去一些无关紧要的代码...
...for(i=0;i<thread_num;i++){//创建线程
int ret;
s[i]=0;
if(i==0){ret = pthread_create(&threads[i], NULL, (void *)C_thread1, NULL);}
else {ret = pthread_create(&threads[i], NULL, C_thread2, (void*)i);}
}
for(i=0;i<thread_num;i++){
pthread_join(threads[i], NULL);
}
end=clock();
duration =(double) (end- start)/CLOCKS_PER_SEC;
printf("%lf\n",duration);
} |
|