vi test_printf.c
#include<stdio.h>
#include<pthread.h>
#include<math.h>
#include<unistd.h>
#define NBR_THRS 8
void* busywork(void* arg) {
long t = (long) arg;
double res = 0;
for(long i =0; i< t; i++)
res += sin(i) * cos(i);
//printf("res=%f\n", res);
return NULL;
}
int main() {
pthread_t ID[NBR_THRS];
long i;
for(i=0; i<NBR_THRS; i++)
pthread_create(&ID[i], NULL, busywork, (void*) 100000000);
for(i=0; i<NBR_THRS; i++)
pthread_join(ID[i], NULL);
}
本程序若去除子程序中的打印语句,在我的电脑上运行仅花0.002秒;若加上子程序中的打印语句,则花20.635秒之多!请大侠们解释下为啥差别那么大?编译都用 gcc -Wall -O3 demo.c -lpthread -lm
版权声明:本文为ccjsj1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。