这就是算法,但是当我要测量的执行时间,它给了我零。为什么呢?
This is the "algorithm", but when I want to measure the execution time it gives me zero. Why?
#define ARRAY_SIZE 10000
...
clock_t start, end;
start = clock();
for( i = 0; i < ARRAY_SIZE; i++)
{
non_parallel[i] = vec[i] * vec[i];
}
end = clock();
printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );
所以,我应该怎么做来测量时间?
So What should i do to measure the time?
两件事情:
10000
不是很多现代的计算机上。因此,该循环将运行大概不到一毫秒 - 比时钟precision以下()
。因此,它会返回零。
10000
is not a lot on a modern computer. Therefore that loop will run in probably less than a millisecond - less than the precision of clock()
. Therefore it will return zero.
如果您不使用 non_parallel
的可能,整个循环将被优化掉的编译器。
If you aren't using the result of non_parallel
its possible that the entire loop will be optimized out by the compiler.
最有可能的,你只需要一个更昂贵的循环。尝试增加 ARRAY_SIZE
的东西要多得多。
Most likely, you just need a more expensive loop. Try increasing ARRAY_SIZE
to something much larger.
这是一个测试我的机器上有一个较大的数组大小:
#define ARRAY_SIZE 100000000
int main(){
clock_t start, end;
double *non_parallel = (double*)malloc(ARRAY_SIZE * sizeof(double));
double *vec = (double*)malloc(ARRAY_SIZE * sizeof(double));
start = clock();
for(int i = 0; i < ARRAY_SIZE; i++)
{
non_parallel[i] = vec[i] * vec[i];
}
end = clock();
printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );
free(non_parallel);
free(vec);
return 0;
}
输出:
Number of seconds: 0.446000