jdhedden has asked for the wisdom of the Perl Monks concerning the following question:

I have found a problem with Time::HiRes under Cygwin. Seems that the Time::HiRes::time() function slowly falls behind Perl's core time() function. Running the following for an hour or so illustrates this on my PC, especially when it is under load.
#!/usr/bin/perl use strict; use warnings; use Time::HiRes; while (1) { print("Core : ", scalar localtime(), "\n"); print("HiRes: ", scalar localtime(Time::HiRes::time), "\n\n"); sleep(60); }

Update:I found the source of the problem.

PCs are terrible time keepers. It seems that under heavy load, the clock can continually loose time. On my laptop, I run an NTP client every 5 minutes to compensate for this. Doing so then causes problems with Time::HiRes. Whereas Perl's core time() function picks up on the NTP clock adjustments, Time::HiRes::time() does not.

You can see this by running the script above with sleep(10). Then adjust your PC's clock. You'll see that the 'Core' time picks up the adjustment, but the HiRes time does not.

Whether this constitutes a bug in Time::HiRes or not is a question.

Replies are listed 'Best First'.
Re: Time::HiRes::time() Bug on Cygwin
by Limbic~Region (Chancellor) on Apr 03, 2004 at 14:13 UTC
    jdhedden,
    You do not say how far behind. Reading the docs:

    NOTE 1: This higher resolution timer can return values
    either less or more than the core time(), depending on
    whether your platform rounds the higher resolution timer
    values up, down, or to the nearest second to get the core
    time(), but naturally the difference should be never more
    than half a second.

    It would not be unrealistic to see the times off due to rounding issues. Additionally, you do not specify which OS you are running Cygwin on. It is my understanding the granularity of sleep may be determined by the kernel. I was not able to reproduce your problem using the following code beyond the expected discrepency:
    #!/usr/bin/perl use strict; use warnings; use Time::HiRes 'usleep'; while (1) { print "Core : ", CORE::time, "\n"; print "HiRes: ", Time::HiRes::time, "\n\n"; usleep (60_000_000); }
    Cheers - L~R
      It is not a rounding problem. The error gets increasingly longer at a rate of several seconds per hour.

      This is on Windows 98. I don't see how it could be a granularity issue. The problem is that the error keeps accumulating.

      Using your code, I was able to preproduce the problem:

      Core : 1081092725 HiRes: 1081092725.097 Core : 1081092785 HiRes: 1081092785.098 Core : 1081092849 HiRes: 1081092845.099 Core : 1081092908 HiRes: 1081092905.1 Core : 1081092968 HiRes: 1081092965.105 Core : 1081093029 HiRes: 1081093025.106 Core : 1081093088 HiRes: 1081093085.162
      Here it shows a discrete jump of about 4 seconds. No idea why.