我在C中编写了一个代码,用于查找两个parabloids之间的数量。虽然在我的时钟上检查时代码运行大约需要7秒,但使用内置时钟功能只需2.6秒。
在并行化后使用openmp运行相同的代码,需要7秒钟。有人可以指出我的时间缺陷吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main ()
{
int i, j, k;
double xmin = 0;
double xmax = 0.707;
long Nx = 10000, Ny, n;
double dx = (xmax - xmin)/(Nx-1);
double x[Nx];
double Nelemy[Nx];
double y, z1, z2;
double vol1[Nx], vol2[Nx];
double sum1 = 0, sum2 = 0;
double start, end, elapsedTime;
start = clock();
// Fill in the array containing values at x nodes and number of elements at in y at each node of x
for (i=0; i<Nx; i++)
{
x[i] = 0;
x[i] = i*dx;
y = sqrt(1 - 2*x[i]*x[i]);
n = y/dx;
Ny = ceil(n) + 1;
Nelemy[i] = 0;
Nelemy[i] = Ny;
}
// Calculate area for each cell
double dA = dx*dx;
// Calculate the value of z for each cell
for (i=0; i<Nx; i++)
{
vol1[i] = 0;
vol2[i] = 0;
Ny = Nelemy[i];
double Y[Ny];
for (k=0; k<Ny; k++)
{
Y[k] = 0;
Y[k] = k*dx;
}
for (j=0; j<Ny; j++)
{
z1 = 5 * ( x[i] * x[i] + Y[j] * Y[j]);
z2 = 6 - 7 * x[i] * x[i] - Y[j] * Y[j];
// Calculate the partial volume
vol1[i] = vol1[i] + dA * z1;
vol2[i] = vol2[i] + dA * z2;
}
}
// print the contents of vol
for (i=0; i<Nx; i++)
{
sum1 += vol1[i];
sum2 += vol2[i];
}
end = clock();
elapsedTime = (double)(end - start) / CLOCKS_PER_SEC;
printf("The enclosed Volume is: %lf\n", (sum2 - sum1));
printf("The total time taken is: %lf\n", elapsedTime);
return 0;
}