gpt4 book ai didi

c - 为什么n个线程的平均速度不如C中一个单线程的速度?

转载 作者:太空宇宙 更新时间:2023-11-04 05:24:24 25 4
gpt4 key购买 nike

我写了一个程序,有 2 个线程做同样的事情,但我发现每个线程的吞吐量比我只生成一个线程的吞吐量要慢。然后我编写了这个简单的测试,看看这是我的问题还是系统的问题。

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


/*
* Function: run_add
* -----------------------
* Do addition operation for iteration ^ 3 times
*
* returns: void
*/
void *run_add(void *ptr) {
clock_t t1, t2;
t1 = clock();

int sum = 0;
int i = 0, j = 0, k = 0;
int iteration = 1000;
long total = iteration * iteration * iteration;
for (i = 0; i < iteration; i++) {
for (j = 0; j < iteration; j++) {
for (k = 0; k < iteration; k++) {
sum++;
}
}
}

t2 = clock();
float diff = ((float)(t2 - t1) / 1000000.0F );
printf("thread id = %d\n", (int)(pthread_self()));
printf("Total addtions: %ld\n", total);
printf("Total time: %f second\n", diff);
printf("Addition per second: %f\n", total / diff);
printf("\n");

return NULL;
}


void run_test(int num_thread) {
pthread_t pth_arr[num_thread];
int i = 0;
for (i = 0; i < num_thread; i++) {
pthread_create(&pth_arr[i], NULL, run_add, NULL);
}

for (i = 0; i < num_thread; i++) {
pthread_join(pth_arr[i], NULL);
}
}

int main() {
int num_thread = 5;
int i = 0;
for (i = 1; i < num_thread; i++) {
printf("Running SUM with %d threads. \n\n", i);
run_test(i);
}
return 0;
}

结果仍然显示 n 个线程的平均速度比单个线程慢。我拥有的线程越多,每个线程就越慢。

结果如下:

Running SUM with 1 threads.

thread id = 528384,Total addtions: 1000000000,Total time: 1.441257 second,Addition per second: 693838784.000000

Running SUM with 2 threads.

thread id = 528384,Total addtions: 1000000000,Total time: 2.970870 second,Addition per second: 336601728.000000

thread id = 1064960,Total addtions: 1000000000,Total time: 2.972992 second,Addition per second: 336361504.000000

Running SUM with 3 threads.

thread id = 1064960,Total addtions: 1000000000,Total time: 4.434701 second,Addition per second: 225494352.000000

thread id = 1601536,Total addtions: 1000000000,Total time: 4.449250 second,Addition per second: 224756976.000000

thread id = 528384,Total addtions: 1000000000,Total time: 4.454826 second,Addition per second: 224475664.000000

Running SUM with 4 threads.

thread id = 528384,Total addtions: 1000000000,Total time: 6.261967 second,Addition per second: 159694224.000000

thread id = 1064960,Total addtions: 1000000000,Total time: 6.293107 second,Addition per second: 158904016.000000

thread id = 2138112,Total addtions: 1000000000,Total time: 6.295047 second,Addition per second: 158855056.000000

thread id = 1601536,Total addtions: 1000000000,Total time: 6.306261 second,Addition per second: 158572560.000000

我有一个 4 核 CPU,我的系统监视器显示每次我运行 n 个线程时,n 个 CPU 内核都被 100% 使用。 n 个线程(<= 我的 CPU 内核)的运行速度应该是一个线程的 n 倍,这是真的吗?为什么这里不是这种情况?

最佳答案

clock() 测量 CPU 时间而不是“墙”时间。它还测量所有线程的总时间..

CPU 时间是处理器执行代码的时间,wall time 是真实世界的运行时间(就像墙上的时钟显示的那样)

使用/usr/bin/time 为你的程序计时,看看到底发生了什么。或者使用像 time()、gettimeofday() 或 clock_gettime() 这样的挂钟函数

clock_gettime() 可以测量此线程、此进程或挂钟时间的 CPU 时间。 - 这可能是进行此类实验的最佳方式。

关于c - 为什么n个线程的平均速度不如C中一个单线程的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com