| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 673 人关注过本帖
标题:多线程编程,为何用了多线程时间反而暴增?
只看楼主 加入收藏
Fanpu_Xue
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-6-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
多线程编程,为何用了多线程时间反而暴增?
这是一个使用蒙特卡洛方法计算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


不知道为什么会这样?初入多线程,望大神指点!
搜索更多相关主题的帖子: int 多线程 double the Total 
2023-06-08 12:23
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
不明白为什么不肯贴完整代码?虽然手补几个 #include 也不算多,但这种无意义的故意浪费时间目的为何?

懒得多讲了,直接说结论:浪费在rand上(C语言rand不保证为线程安全),既然你用linux的话,用 rand_r 替代之。
2023-06-08 14:04
Fanpu_Xue
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2023-6-8
收藏
得分:0 
好的,解决了,谢谢
2023-06-08 14:21
快速回复:多线程编程,为何用了多线程时间反而暴增?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037167 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved