From: lars@... Date: 2017-07-17T15:39:33+00:00 Subject: [ruby-dev:50177] [Ruby trunk Feature#13732] Precise Time.now on Windows Issue #13732 has been updated by larskanis (Lars Kanis). Thanks @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. ---------------------------------------- Feature #13732: Precise Time.now on Windows https://bugs.ruby-lang.org/issues/13732#change-65819 * Author: kubo (Takehiro Kubo) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- 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) -- https://bugs.ruby-lang.org/