多线程编程,为何用了多线程时间反而暴增?
这是一个使用蒙特卡洛方法计算Pi的任务,希望查看不同随机点数下对估算pi的准确性未使用多线程的代码:
程序代码:
void cal_pi(int intervals)
{
unsigned int seed = time(NULL);
int in_circle_points = 0;
int in_square_points = 0;
for (int i=0; i<intervals*intervals; ++i)
{
double x = (double)rand()/RAND_MAX;
double y = (double)rand()/RAND_MAX;
if (x*x + y*y <= 1) in_circle_points += 1;
in_square_points += 1;
}
double pi = 4.0 * in_circle_points / in_square_points;
printf("Total Points: %d, the points in circle: %d, Pi:%lf\n", in_circle_points, in_square_points, pi);
}
int main(int argc, const char *argv[])
{
clock_t start, end;
double time_used;
srand(time(NULL));
start = clock();
for (int i=0; i<10; ++i)
{
cal_pi(100*(i+1));
}
end = clock();
printf("Total Used Time: %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
return 0;
}
使用多线程的代码:
程序代码:
void* cal_pi(void* intervals)
{
unsigned int seed = time(NULL);
int in_circle_points = 0;
int in_square_points = 0;
int num = *((int*)intervals);
for (int i=0; i<num*num; ++i)
{
double x = (double)rand()/RAND_MAX;
double y = (double)rand()/RAND_MAX;
if (x*x + y*y <= 1) in_circle_points += 1;
in_square_points += 1;
}
double pi = 4.0 * in_circle_points / in_square_points;
printf("Total Points: %d, the points in circle: %d, Pi:%lf\n", in_circle_points, in_square_points, pi);
pthread_exit(0);
}
int main(int argc, const char *argv[])
{
clock_t start, end;
double time_used;
srand(time(NULL));
start = clock();
pthread_t cal_thread[10];
int args[10];
for (int i=0; i<10; ++i)
{
args[i] = 100*(i+1);
pthread_create(cal_thread+i, NULL, cal_pi, args+i);
}
for (int i=0; i<10; ++i)
{
pthread_join(cal_thread[i], NULL);
}
end = clock();
printf("Total Used Time: %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
return 0;
}
没有使用多线程的运行结果:
程序代码:Total Points: 7887, the points in circle: 10000, Pi:3.154800 Total Points: 31487, the points in circle: 40000, Pi:3.148700 Total Points: 70776, the points in circle: 90000, Pi:3.145600 Total Points: 125450, the points in circle: 160000, Pi:3.136250 Total Points: 196211, the points in circle: 250000, Pi:3.139376 Total Points: 282642, the points in circle: 360000, Pi:3.140467 Total Points: 384858, the points in circle: 490000, Pi:3.141698 Total Points: 502177, the points in circle: 640000, Pi:3.138606 Total Points: 636192, the points in circle: 810000, Pi:3.141689 Total Points: 785339, the points in circle: 1000000, Pi:3.141356 Total Used Time: 0.097244 seconds ./mul 0.10s user 0.00s system 99% cpu 0.099 total
使用了多线程的运行结果:
程序代码:Total Points: 7906, the points in circle: 10000, Pi:3.162400 Total Points: 31469, the points in circle: 40000, Pi:3.146900 Total Points: 70687, the points in circle: 90000, Pi:3.141644 Total Points: 125873, the points in circle: 160000, Pi:3.146825 Total Points: 195919, the points in circle: 250000, Pi:3.134704 Total Points: 282496, the points in circle: 360000, Pi:3.138844 Total Points: 384970, the points in circle: 490000, Pi:3.142612 Total Points: 502715, the points in circle: 640000, Pi:3.141969 Total Points: 635928, the points in circle: 810000, Pi:3.140385 Total Points: 785658, the points in circle: 1000000, Pi:3.142632 Total Used Time: 3.574630 seconds ./thread 0.75s user 2.83s system 485% cpu 0.737 total
不知道为什么会这样?初入多线程,望大神指点!







