Ç如何正确测量时间? [英] C how to measure time correctly?

查看:234
本文介绍了Ç如何正确测量时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是算法,但是当我要测量的执行时间,它给了我零。为什么呢?

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?

推荐答案

两件事情:

  1. 10000 不是很多现代的计算机上。因此,该循环将运行大概不到一毫秒 - 比时钟precision以下()。因此,它会返回零。

  1. 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

这篇关于Ç如何正确测量时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆