单线程与C中的多线程通过pthread

时间:2018-03-15 15:02:15

标签: c multithreading performance pthreads

我在这里要做的是通过简单和重复的操作来比较单线程和多线程的性能。所以我有两个线程将这个随机数0xde分配给这个数组,每个线程占用数组的第一个和第二个半,而单线程从索引0到结尾自己做同样的工作。

我不明白的是,尽管这些子线程的工作量是单线程(即主线程)的一半,但它们需要更多时间来完成任务!我不希望它花费一半的时间作为单线程,但我无法想象为什么它需要比单线程更长的时间。

更令人惊讶的是,如果我先将订单切换为单线程,那么我会得到我想要的结果。我真的可以在这方面使用一些帮助,因为这一切都搞砸了。提前谢谢!

PS。我正在使用Raspberry Pi 3,它有4个ARM处理器,如果有帮助的话。     这是我得到的结果。     多线程1:46 ms
    Mulththreading2:50 ms
    单线程:34 ms

#include <pthread.h>
#include <stdio.h>
#include <time.h>
#define SIZE 1000000

clock_t difference = 0;
clock_t difference1 = 0;
clock_t difference2 = 0;

void *substitute1(void *operand)
{
    int *arr = (int *)operand;
    int i=0;
    clock_t before1 = clock();

    for(i=0;i<(SIZE/2);i++)
    {
        arr[i] = 0x00de;
    }
    difference1 = clock() - before1;
    return NULL;
}


void *substitute2(void *operand)
{
    int *arr = (int *)operand;
    int i=0;
    clock_t before2 = clock();

    for(i=(SIZE/2);i<SIZE;i++)
    {
        arr[i] = 0x00de;
    }
    difference2 = clock() - before2;
    return NULL;
}

void single_thread(int *arr);

int main(void)
{
    int arr[SIZE];
    int test[SIZE];
    int msec1, msec2;


    // declare thread variables
    pthread_t thread1;
    pthread_t thread2;

    // create threads
    pthread_create(&thread1, NULL, substitute1, arr);
    pthread_create(&thread2, NULL, substitute2, arr);

    // wait untill the two threads do all their work
    while(arr[SIZE/2 - 1] != 0x00de) {/*printf("arr[%d] = %x\n", SIZE/2 - 1, arr[SIZE/2 -1]);*/};
    while(arr[SIZE-1] != 0x00de) {/*printf("arr[%d] = %x\n", SIZE-1, arr[SIZE-1]);*/};

    // and then join
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    // convert clocks to milliseconds
    msec1 = difference1 * 1000 / CLOCKS_PER_SEC;
    msec2 = difference2 * 1000 / CLOCKS_PER_SEC;

    printf("Multithreading1 : %d ms\n", msec1);
    printf("Mulththreading2 : %d ms\n", msec2);

    // here's the single-threading
    single_thread(test);

    return 0;
}

void single_thread(int *arr) 
{
    int msec = 0, i = 0;

    // declare initial clock
    clock_t single_before = clock();

    for(i=0;i<SIZE;i++)
    {
        arr[i] = 0x00de;
    }

    difference = clock() - single_before;

    // convert clocks to milliseconds
    msec = difference * 1000 / CLOCKS_PER_SEC;
    printf("Singlethreading : %d ms\n", msec);

}

2 个答案:

答案 0 :(得分:3)

多线程程序的性能改进来自于在多个处理单元之间分配工作负载。因此,您的程序必须使用足够的处理器来证明分配工作负载的合理性。但是,您在这里所做的只是将数据写入内存,没有进行任何处理,因此您受到内存访问的约束,如here所述。

答案 1 :(得分:2)

您可以使用大量数据来测量多线程的性能。使用非常少量的数据,您无法测量多线程应用程序的性能。原因: -

正如您所说,您的系统中有4个处理器,它们足以衡量您案例中2个线程的性能。但是为什么它比单线程花费更多的时间。

  
      
  1. 创建线程O / S需要为每个线程分配内存,这需要花费时间(即使它很小)。
  2.   
  3. 当您创建多线程时,它需要上下文切换,这也需要时间。
  4.   
  5. 需要释放分配给线程的内存,这也需要时间。
  6.   

因此,当你尝试使用多线程的小操作时,它的性能将与单线程相同,甚至更不适合。因此,在这种情况下,您的结果是前言。要测量多线程架构的性能,请使用大量数据并进行复杂操作,然后才能看到差异。

现在只是为了理解,请参阅以下场景。只需考虑睡眠是函数完成任务所需的总时间: -

如下所示,您可以看到差异: -

void callme()
{
   printf("In callme()\n");
   sleep(2);
}

void main()
{
    //read the system time here
    callme();
    callme();
    callme();
    callme();
    callme();
    //read the system time here and check how much time it took in a single thread architecture 
    //it will take more than 10 sec
}

现在尝试使用多线程架构: -

void * callme(void *)
{
   printf("In callme()\n");
   sleep(2);
   return NULL; //better use pthread_exit(NULL);
}


void main()
{
    //read the system time here
    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    pthread_t thread4;
    pthread_t thread5;


    pthread_create(&thread1, NULL, callme, NULL);
    pthread_create(&thread2, NULL, callme, NULL);
    pthread_create(&thread3, NULL, callme, NULL);
    pthread_create(&thread4, NULL, callme, NULL);
    pthread_create(&thread5, NULL, callme, NULL);

        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);
        pthread_join(thread3, NULL);
        pthread_join(thread4, NULL);
        pthread_join(thread5, NULL);   

      //read the system time here and check how much time it took in a single thread

    //it will take hardly 2.5 to 3 seconds benefit of 7 to 7.5 second than single thread
}

希望这有助于您理解。

相关问题