时钟滴答和时钟周期的概念 [英] Concept of clock tick and clock cycles

查看:283
本文介绍了时钟滴答和时钟周期的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个很小的代码来测量我的乘法算法所花费的时间:

I have written a very small code to measure the time taken by my multiplication algorithm :

  clock_t begin, end;
  float time_spent;

begin = clock();
a = b*c;
end = clock();
time_spent = (float)(end - begin)/CLOCKS_PER_SEC;

我正在Windows下使用mingw。

I am working with mingw under Windows.

我猜测 end = clock()会给我那一刻的时钟滴答声。从开始减去它会给我乘法消耗的时钟滴答。当我用 CLOCKS_PER_SEC 除时,我将获得总的时间。

I am guessing that end = clock() will give me the clock ticks at that particular moment. Subtracting it from begin will give me clock ticks consumed by multiplication. When I divide with CLOCKS_PER_SEC, I will get the total amount of time.

我的第一个问题是:是否有时钟滴答和时钟周期之间的差异?

My first question is: Is there a difference between clock ticks and clock cycle?

我的算法很小,以至于 end-begin 是0。这是否意味着我的代码执行时间少于1个滴答,这就是为什么我得到零的原因?

My algorithm here is so small that the difference end-begin is 0. Does this mean that my code execution time was less than 1 tick and that's why I am getting zero?

推荐答案


我的第一个问题是:时钟滴答和时钟周期之间有区别吗?

My first question is: Is there a difference between clock ticks and clock cycle?

是的。时钟滴答可能是1毫秒或微秒,而时钟周期可能是0.3纳秒。在POSIX系统上,必须将 CLOCKS_PER_SEC 定义为 1000000 (100万)。请注意,如果无法以微秒的分辨率获得CPU测量值,则 clock()的返回值中的最小跳变将大于1。

Yes. A clock tick could be 1 millisecond or microsecond while the clock cycle could be 0.3 nanoseconds. On POSIX systems CLOCKS_PER_SEC must be defined as 1000000 (1 million). Note that if the CPU measurement cannot be obtained with microsecond resolution then the smallest jump in the return value from clock() will be larger than one.


我的算法是如此之小,以至于始末差为0。这是否意味着我的代码执行时间少于1个tick,这就是为什么我得到零的原因?

My algorithm here is so small that the difference end-begin is 0. Does this mean that my code execution time was less than 1 tick and that's why I am getting zero?

是。为了获得更好的阅读效果,我建议您循环进行足够的迭代,以便在几秒钟内进行测量。

Yes. To get a better reading I suggest that you loop enough iterations so that you measure over several seconds.

这篇关于时钟滴答和时钟周期的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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