【问题标题】:Why does the program that uses a thread takes more time?为什么使用线程的程序需要更多时间?
【发布时间】:2013-10-18 20:08:51
【问题描述】:

我添加了一个线程来计算第一个数组的 3,而主进程生成第二个数组。我认为这会使程序更快,但在 Linux 上使用 time 命令后,带线程的一个用了 0m7.627s,另一个用了 0m5.701s。 起初我以为我使用了一个非常小的长度,并且由于创建线程的时间而变得更大,但事实并非如此。时间差与长度成正比... 这仅适用于更多线程吗? (也许在另一个例子中) 难道我做错了什么? 另外我不明白 pthread_join(..., this) 的第二个参数是如何工作的,我尝试了很多不同的方式,但它从来没有工作过。我的帮助会很棒,谢谢。

没有线程:

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

#define LENGTH 100000000

void * count3s(void * i){
    int numberOf3 = 0;  
    int * j = (int *) i;
    int counter = 0;
    for(counter = 0; counter < LENGTH; counter++){
        if(*(j+counter) == 3){
            numberOf3++;
        }
    }
    *((int *) i) = numberOf3;
    return i;
}


int main(int argc, char *argv[]){
    pthread_t p0, p1;
    int * i = (int *) malloc(sizeof(int)*LENGTH);
    int * j = (int *) malloc(sizeof(int)*LENGTH);
    int c = 0, d = 0;
    srand(0);
    for(c=0;c<LENGTH;c++){
        *(i+c) = rand() % 4;
    }

    for(c=0;c<LENGTH;c++){
        *(j+c) = rand() % 4;
    }

    d = *((int *) count3s((void *) i));
    c = *((int *) count3s((void *) j));
    printf("C:%d, D:%d\n", c, *i);
    return 0;
}

有线程:

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

#define LENGTH 100000000

void * count3s(void * i){
    int numberOf3 = 0;  
    int * j = (int *) i;
    int counter = 0;
    for(counter = 0; counter < LENGTH; counter++){
        if(*(j+counter) == 3){
            numberOf3++;
        }
    }
    *((int *) i) = numberOf3;
    return i;
}


int main(int argc, char *argv[]){
    pthread_t p0, p1;
    int * i = (int *) malloc(sizeof(int)*LENGTH);
    int * j = (int *) malloc(sizeof(int)*LENGTH);
    int c = 0, d = 0;
    srand(0);
    for(c=0;c<LENGTH;c++){
        *(i+c) = rand() % 4;
    }
    //thread starts counting 3's
    pthread_create(&p0, NULL, count3s,(void *)i);       //thread created
    for(c=0;c<LENGTH;c++){
        *(j+c) = rand() % 4;
    }

    pthread_join(p0, NULL);     
    c = *((int *) count3s((void *) j));
    printf("C:%d, D:%d\n", c, *i);
    return 0;
}

【问题讨论】:

  • 尝试将您的 pthread_join 呼叫移动到第二个 count3s 呼叫下方。就目前而言,您正在强制在第二次计数开始之前完成第一次计数。您也可以将第一个随机生成循环移动到线程中,以便生成和计数可以并行运行。
  • 我尝试了你的第一个建议,没有任何改变。第二个没看懂?这不会是分段错误错误吗?编辑:刚刚尝试了第二个 + 第一个,更糟的是,现在需要 10 秒。我认为它等待另一个完成。但是感谢您的帮助。有什么想法吗?
  • 我无法重现结果。在我的机器上没有可测量的差异。
  • 你测量了多少次执行时间?您应该进行多次测量,以减少操作系统中断您执行更重要任务的可能性,从而导致结果出现偏差。
  • 我做了很多,greeny,它总是一样的。 Timo 尝试增加长度,或者按照 Nathan 说的做……这可能是我电脑的问题。不过,我对此表示怀疑。

标签: c multithreading pthreads


【解决方案1】:

因为创建线程不是免费的

【讨论】:

  • @SadSeven - 这没有任何意义。
  • 我知道,哈哈...但是用更小的 LENGTHS 甚至不需要一秒钟,现在大约需要 4 秒。
  • 处理器在做什么?使用调试器
  • 可能它只是使用单个内核进行所有处理。您在这里使用 int 指针共享变量。也许如果您要一次生成所有这些,然后在单独的线程中进行计数会显示出差异?自己尝试过之后,我注意到处理时间减少了 1 秒多一点。
【解决方案2】:

如果您修改 main 以一次生成所有随机数,则处理每个数组 分开你会看到改进。

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

#define LENGTH 100000000

void * count3s(void * i){
   int numberOf3 = 0;
   int * j = (int *) i;
   int counter = 0;
   for(counter = 0; counter < LENGTH; counter++){
      if(*(j+counter) == 3){
         numberOf3++;
      }
   }
   *((int *) i) = numberOf3;
   return i;
}

int main(int argc, char *argv[]){
   pthread_t p0, p1;
   int * i = (int *) malloc(sizeof(int)*LENGTH);
   int * j = (int *) malloc(sizeof(int)*LENGTH);
   int c = 0, d = 0;
   srand(0);
   for(c=0;c<LENGTH;c++){
      *(i+c) = rand() % 4;
      *(j+c) = rand() % 4;
   }
   //thread starts counting 3's
   pthread_create(&p1, NULL, count3s,(void *)j);
   pthread_create(&p0, NULL, count3s,(void *)i);       //thread created

   pthread_join(p1, NULL);
   pthread_join(p0, NULL);
   printf("C:%d, D:%d\n", *j, *i);
   return 0;
}

这给了我结果 新结果:

threads
C:24999967, D:24998864

real    0m2.994s
user    0m3.064s
sys     0m0.452s

threadless
C:24996371, D:25002460

real    0m3.510s
user    0m3.196s
sys     0m0.300s

这是一个小的改进,但它是一个改进。

上一个结果:

threads-wrong
C:24996371, D:25002460

real    0m3.840s
user    0m4.032s
sys     0m0.328s
threadless
C:24996371, D:25002460

real    0m3.518s
user    0m3.148s
sys     0m0.356s

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-20
    • 2018-08-03
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多