Project

General

Profile

Feature #13732

Precise Time.now on Windows

Added by kubo (Takehiro Kubo) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-dev:50167]

Description

Windows 上での Time.now の精度を調べてみたところ、システムの
タイマー精度(1ミリ秒以上15.6ミリ秒以下。通常は15.6ミリ秒)と同じに
なっていました。

試験内容と結果は以下のURLを参照
https://github.com/MSP-Greg/ruby_on_windows/issues/1#issuecomment-313660247

Time.now は内部では GetSystemTimeAsFileTime を使って現在時刻を取得しています。
代わりに GetSystemTimePreciseAsFileTime を使うようにすれば、
1マイクロ秒以下の精度で現在時刻を取得できます。ただし、GetSystemTimePreciseAsFileTime
が使えるのは Windows 8、Windows Server 2012 以降です。

添付のパッチでは GetSystemTimePreciseAsFileTime があったらそれを使い、
なかったらこれまで通りに GetSystemTimeAsFileTime を使うようになっています。

パッチを適用して、Windows 10 で上記URL内のスクリプトを実行したところ、以下の結果になりました。

loop 10 times
1.0e-06
2.0e-05
2.0e-06
2.0e-06
2.0e-06
1.0e-06
1.0e-06
2.0e-06
1.0e-06
1.0e-06
min: 1.0e-06
man: 2.0e-05
avg: 3.3e-06

loop 10 times となっているので Time.now を実行する度に違う値を
戻していて、1ループで数マイクロ秒かかっています。


Files

precise-time-now.dif (1.09 KB) precise-time-now.dif kubo (Takehiro Kubo), 07/08/2017 07:54 AM

Associated revisions

Revision 6a832cdb
Added by usa (Usaku NAKAMURA) over 1 year ago

Use GetSystemTimePreciseAsFileTime on recent Windows

  • win32/win32.c (gettiemeofday, wutime): use GetSystemTimePreciseAsFileTime instead of GetSystemTimeAsFileTime if it is available. This patch is based on Takehiro Kubo kubo@jiubao.org 's one (change only the name of wrapper function). Thanks! and sorry to late [ruby-dev:50167] [Feature #13732]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60240 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 60240
Added by usa (Usaku NAKAMURA) over 1 year ago

Use GetSystemTimePreciseAsFileTime on recent Windows

  • win32/win32.c (gettiemeofday, wutime): use GetSystemTimePreciseAsFileTime instead of GetSystemTimeAsFileTime if it is available. This patch is based on Takehiro Kubo kubo@jiubao.org 's one (change only the name of wrapper function). Thanks! and sorry to late [ruby-dev:50167] [Feature #13732]

Revision 60240
Added by usa (Usaku NAKAMURA) over 1 year ago

Use GetSystemTimePreciseAsFileTime on recent Windows

  • win32/win32.c (gettiemeofday, wutime): use GetSystemTimePreciseAsFileTime instead of GetSystemTimeAsFileTime if it is available. This patch is based on Takehiro Kubo kubo@jiubao.org 's one (change only the name of wrapper function). Thanks! and sorry to late [ruby-dev:50167] [Feature #13732]

Revision 60240
Added by usa (Usaku NAKAMURA) over 1 year ago

Use GetSystemTimePreciseAsFileTime on recent Windows

  • win32/win32.c (gettiemeofday, wutime): use GetSystemTimePreciseAsFileTime instead of GetSystemTimeAsFileTime if it is available. This patch is based on Takehiro Kubo kubo@jiubao.org 's one (change only the name of wrapper function). Thanks! and sorry to late [ruby-dev:50167] [Feature #13732]

History

Updated by larskanis (Lars Kanis) over 1 year ago

Thanks kubo (Takehiro Kubo)! Your patch looks good. Finally precise timestamps on Windows! The 1/64 granularity was annoying. I tested your patch successfully on Windows-10 64 bit. Time.now returns reliable usec precision as expected.

Updated by nagachika (Tomoyuki Chikanaga) over 1 year ago

  • Assignee set to usa (Usaku NAKAMURA)
#3

Updated by naruse (Yui NARUSE) over 1 year ago

  • Target version set to 2.5
#4

Updated by usa (Usaku NAKAMURA) over 1 year ago

  • Status changed from Open to Closed

Applied in changeset trunk|r60240.


Use GetSystemTimePreciseAsFileTime on recent Windows

  • win32/win32.c (gettiemeofday, wutime): use GetSystemTimePreciseAsFileTime instead of GetSystemTimeAsFileTime if it is available. This patch is based on Takehiro Kubo kubo@jiubao.org 's one (change only the name of wrapper function). Thanks! and sorry to late [ruby-dev:50167] [Feature #13732]

Also available in: Atom PDF