我正在学习使用Pthreads进行并行处理.我有一个四核处理器.不幸的是,以下代码的并行化部分运行速度比非并行化代码慢大约5倍.我在这做错了什么?在此先感谢您的帮助.
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <stdlib.h>
#define NTHREADS 4
#define SIZE NTHREADS*10000000
struct params {
int * arr;
int sum;
};
/* The worker function for the pthreads */
void * myFun (void * x){
int i;
struct params * b = (struct params *) x;
for (i = 0; i < (int)(SIZE/NTHREADS); ++i){
b->sum += b->arr[i];
}
return NULL;
}
/* unparallelized summing function*/
int arrSum(int * arr, int size){
int sum = 0;
for (int …Run Code Online (Sandbox Code Playgroud)