What is the maximum precision of the Timer in .NET?

后端 未结 6 2043
北荒
北荒 2020-12-20 14:12

I\'m wondering what the precision of the Timer class is in System.Timers, because it\'s a double (which would seem to indicate that you can have fractions of milliseconds).

6条回答
  •  有刺的猬
    2020-12-20 14:52

    Timer resolution goes down into the 1ms regime without much implementation effort. I have just described the reason for the double precision in this answer. When configuring the a system to run at 1024 interrupts per second (using the multimedia timer API) the time beween interrupts is 0.9765625 ms. The standard resolution for timing matters is 100ns. Those values are stored as integers. The value 0.9765625 cannot be stored without loosing accuracy in an integer at 100 ns resolution. The last digit (5) represents 500 ps. Thus the resolution has to be three orders of magnitude higher. Storing these time values in integers with 100 ps resolution is hopeless since a 8 byte integer at 100 ps resolution would wrap afer just about 21350 days or about 58 years. This time span is too short to get accepted by anyone (remember the Y2K scenario!).

    See the answer linked to to find out more about the details.

提交回复
热议问题