#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>
#include <time.h>
void func1(float *x, int n) {
for (int ind=0; ind<n; ind++) {
x[ind] = fabsf(x[ind]);
}
}
void func2(float *x, int n) {
#pragma omp parallel for
for (int ind=0; ind<n; ind++) {
x[ind] = fabsf(x[ind]);
}
}
int main(void) {
int TIMES = 10;
int N = 1880000*128;
float *arr = (float*)malloc(N*sizeof(float));
float start_time1 = (float)clock() / CLOCKS_PER_SEC;
for (int t=0; t<TIMES; t++) {
func1(arr, N);
}
float duration1 = (float)clock() / CLOCKS_PER_SEC - start_time1;
float start_time2 = (float)clock() / CLOCKS_PER_SEC;
for (int t=0; t<TIMES; t++) {
func2(arr, N);
}
float duration2 = (float)clock() / CLOCKS_PER_SEC - start_time2;
free(arr);
printf("duration1: %.4f\n", duration1 / TIMES);
printf("duration2: %.4f\n", duration2 / TIMES);
return 0;
}
我使用Ubuntu 14.04附带的GCC(4.8.4)编译:
> gcc -O3 -std=c99 -fopenmp -o test test.c -lm
> ./test
duration1: 0.1090
duration2: 0.3777
为什么func2
会变慢?我在AWS中使用c4.2xlarge实例(8CPU,16GB内存)。