单线程和多线程代码花费相同的时间 [英] Single-threaded and multi-threaded code taking the same time

查看:249
本文介绍了单线程和多线程代码花费相同的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用pthreads,但已经意识到,如果我使用1个线程,或者如果我将任务分成N个线程的1/N,则我的代码将独立地花费相同的时间.为了举例说明,我将代码简化为以下示例:

Ive been using pthreads but have realized that my code is taking the same amount of time independently if i use 1 thread or if i separate the task into 1/N for N threads. To exemplify i reduced my code to this example:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include <boost/progress.hpp>

#define SIZEEXEC 200000000

using namespace boost;
using std::cout;
using std::endl;

typedef struct t_d{
   int  intArg;
} Thread_data;

void* function(void *threadarg)
{
    Thread_data *my_data= (Thread_data *) threadarg; 
    int size= my_data->intArg;

    int i=0;
    unsigned rand_state = 0;
    for(i=0; i<size; i++) rand_r(&rand_state);
    return 0;
}

void withOutThreads(void)
{
    Thread_data* t1= new Thread_data();
    t1->intArg= SIZEEXEC/3;
    function((void *) t1);

    Thread_data* t2= new Thread_data();
    t2->intArg= SIZEEXEC/3;
    function((void *) t2);

    Thread_data* t3= new Thread_data();
    t3->intArg= SIZEEXEC/3;
    function((void *) t3);    
}

void withThreads(void)
{
    pthread_t* h1 = new pthread_t;
    pthread_t* h2 = new pthread_t;
    pthread_t* h3 = new pthread_t;
    pthread_attr_t* atr = new pthread_attr_t;

    pthread_attr_init(atr);    
    pthread_attr_setscope(atr,PTHREAD_SCOPE_SYSTEM);

    Thread_data* t1= new Thread_data();
    t1->intArg= SIZEEXEC/3;
    pthread_create(h1,atr,function,(void *) t1);

    Thread_data* t2= new Thread_data();
    t2->intArg= SIZEEXEC/3;
    pthread_create(h2,atr,function,(void *) t2);

    Thread_data* t3= new Thread_data();
    t3->intArg= SIZEEXEC/3;
    pthread_create(h3,atr,function,(void *) t3);

    pthread_join(*h1,0);
    pthread_join(*h2,0);
    pthread_join(*h3,0);
    pthread_attr_destroy(atr);
    delete h1;
    delete h2;
    delete h3;
    delete atr;
}

int main(int argc, char *argv[])
{
    bool multThread= bool(atoi(argv[1]));

    if(!multThread){
        cout << "NO THREADS" << endl;
        progress_timer timer;
        withOutThreads();
    }
    else {
        cout << "WITH THREADS" << endl;
        progress_timer timer;
        withThreads();
    }

    return 0;
}

要么代码错误,要么系统中存在不允许并行处理的问题.我在Ubuntu 11.10 x86_64-linux-gnu,gcc 4.6,Intel®Xeon(R)CPU E5620 @ 2.40GHz×4上运行

Either the code is wrong or there is something on my system not allowing for parallel processing. I'm running on Ubuntu 11.10 x86_64-linux-gnu, gcc 4.6, Intel® Xeon(R) CPU E5620 @ 2.40GHz × 4

感谢您的任何建议!

给定答案,我意识到(1)progress_timer计时器不允许我测量实际"时间的差异,并且(2)我在功能"中给出的任务似乎不足以使我的机器提供不同的功能1或3个线程的时间(这很奇怪,在两种情况下我都得到10秒左右的时间...).我试图分配内存并使它更重,是的,我看到了不同.尽管我的其他代码更复杂,但是很有可能它仍可以在1个或3个线程的同时运行+.谢谢!

Given the answers i have realized that (1) progress_timer timer did not allow me to measure differences in "real" time and (2) that the task i am giving in "function" does not seem to be enough for my machine to give different times with 1 or 3 threads (which is odd, i get around 10 seconds in both cases...). I have tried to allocate memory and make it heavier and yes, i see a difference. Although my other code is more complex, there is a good chance it still runs +- the same time with 1 or 3 threads. Thanks!

推荐答案

这是预期的.您正在测量的是CPU时间,而不是墙壁时间.

This is expected. You are measuring CPU time, not wall time.

time ./test 1
WITH THREADS
2.55 s


real    0m1.387s
user    0m2.556s
sys     0m0.008s

实时时间少于用户时间,这与您测量的时间相同.实时是您的挂钟显示的内容,用户和sys是用户和内核模式下花费的CPU时间 所有CPU的总和.

Real time is less than user time, which is identical to your measured time. Real time is what your wall clock shows, user and sys are CPU time spent in user and kernel mode by all CPUs combined.

time ./test 0
NO THREADS
2.56 s


real    0m2.578s
user    0m2.560s
sys     0m0.008s

您测量的时间,实时时间和用户时间几乎都相同.

Your measured time, real time and user time are all virtually the same.

这篇关于单线程和多线程代码花费相同的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆