尽管延迟可变,但每步的平均时间是恒定的 [英] Average time per step is constant despite a variable delay

查看:39
本文介绍了尽管延迟可变,但每步的平均时间是恒定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBA上制作了一个细胞自动机(Langton的蚂蚁FYI).在每个步骤中,都有一个 Sleep(delay),其中 delay 是一个变量.我还在 display 函数的末尾添加了 DoEvents ,以确保每个步骤都在屏幕上显示.使用 Timer (计时器),我可以监控平均一个步骤需要多长时间.结果绘制在下面的图形上(Y轴:每步时间(以毫秒为单位).X轴: delay (以毫秒为单位))

I've made a cellular automaton (Langton's ant FYI) on VBA. At each step, there is a Sleep(delay) where delay is a variable. I've also added DoEvents at the end of a display function to ensure that each step is shown on screen. With a Timer I can monitor how long one step require in average. The result is plotted on the graph bellow (Y-axis : Time per step (in ms). X-axis : delay (in ms))

您能告诉我为什么会这样吗?特别是为什么它保持稳定?因为海事组织,我想(或多或少)有一条直线.在整个过程中,我在计算机上不做任何其他事情,就得到了这些结果.

Could you explain to me why it looks like that? Especially why does it remain steady ? Because IMO, I'm suppose to have (more or less) a a straight line. I got these results whitout doing anything else on my computer during the whole process.

在此先感谢您的帮助,

推荐答案

那是因为 Sleep API 基于系统时钟.如果时钟的分辨率低于睡眠时间,则将舍入到最接近系统时钟的分辨率.您也许可以调用 timeGetDevCaps 来查看系统的最小计时器分辨率.

That would be because the Sleep API is based off of the system clock. If the resolution of your clock is lower than the time you are sleeping, then it will round up to the nearest resolution of your system clock. You may be able to call timeGetDevCaps to see the minimum timer resolution of your system.

以这种方式思考.您有一只普通的手表,只包含通常的时/分/秒针(1/1000等没有指针).您想计时半秒,但手表仅以1秒的间隔移动-因此,手表的分辨率为每秒1个刻度.您将不知道实际上已经过去了半秒,直到由于这种分辨率而经过了整整一秒,所以将其四舍五入到下一个刻度.

Think about it this way. You have a normal watch that only includes your usual Hour/Minute/Second hand (no hands for 1/1000, etc). You are wanting to time half a second, but your watch only moves in 1 second intervals - hence your watch's resolution is 1 tick per second. You would not know that half a second has actually passed by until the full second passes by due to this resolution, so it's rounded to the next tick.

这篇关于尽管延迟可变,但每步的平均时间是恒定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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