Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

negative elapsed time?

by saneel (Initiate)
on Aug 08, 2011 at 14:56 UTC ( #919240=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am using Time:HiRes module. I just measuring the before and after time as shown in the script below. However, in many cases the "after time" comes out less than "before time", when I run it on Windows with multi-core CPU's. I don't get this when I am running the below script on a VM which has Linux with only one processor allocated to it. Why could this be happening?
#!/usr/bin/perl -w use Time::HiRes; $count = 1000000; print "\nStarting loop\n"; for ($c = $count; $c >= 1; $c--) { $t3 = 0; $t4 =0; $t5 = 0; #print "\n"; #print "t3: "; printf "%.2f\n",$t3; #print "t4: "; printf "%.2f\n",$t4; #print "\n"; $t3 = Time::HiRes::time(); $t4 = Time::HiRes::time(); $t5 = $t4-$t3; #print "t5: "; printf "%.6f\n",$t5; if($t5<=0) { print"\n Value negative in count: $c"; print "\n t3: "; printf "%.6f",$t3; print "\n t4: "; printf "%.6f\n",$t4; print "\n Negative or zero value: $t5"; } } print "\nEnd of loop\n";

Replies are listed 'Best First'.
Re: negative elapsed time? (versions?)
by BrowserUk (Pope) on Aug 08, 2011 at 15:18 UTC

    I don't get any negative or zero results running your script under AS1007/Vista:

    C:\test>junk14 Starting loop End of loop C:\test>perl -MTime::HiRes -E"say $Time::HiRes::VERSION" 1.9719

    What versions are you running?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi, I am using: perl 5, version 12, subversion 4 (v5.12.4) built for MSWin32-x86-multi-thread This is the sample output I am getting: Value negative in count: 5940052 t3: 1312816368.122099 t4: 1312816368.121128 Negative or zero value: -0.000970840454101563 Value negative in count: 996932 t3: 1312814653.022737 t4: 1312814653.022737 Negative or zero value: 0 Value negative in count: 996929 t3: 1312814653.023484 t4: 1312814653.023484 Negative or zero value: 0 Value negative in count: 996928 t3: 1312814653.024365 t4: 1312814653.024365 Negative or zero value: 0 Value negative in count: 996926 t3: 1312814653.024980 t4: 1312814653.024980 Negative or zero value: 0 Also, I tested in windows production servers, and I am getting these negative or zero values

        Hm. I don't see any problems using 5.14 either:

        C:\perl5.14-64\bin>.\perl.exe \test\junk14.pl Starting loop End of loop

        What versions of Time::HiRes and Windows are you using?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: negative elapsed time?
by FunkyMonk (Chancellor) on Aug 08, 2011 at 15:09 UTC
    Does it make any difference if you change if($t5<=0) to if($t5<0) so you actually check for a negative value?

    Ps I don't see any negative values on my (not-a-vm) linux box.

      Hi, Most of times on windows, I get either negative or zero values, especially when the count value is high.
Re: negative elapsed time?
by Marshall (Canon) on Aug 08, 2011 at 15:40 UTC
    This article explains the limitations of the Time Stamp Counter register on multi-core x86 CPU's: Wiki Time Stamp Counter.

    Evidently not applicable as Hi:Res does something different? See note from BrowserUk. I am unaware of any hardware source for a high resolution time other than this CPU feature. I'd be curious if somebody knows of some alternative. As article mentions, MS provides an API for times, but it uses this hardware.

      I am unaware of any hardware source for a high resolution time other than this CPU feature. I'd be curious if somebody knows of some alternative.

      On windows, Time::HiRes uses QueryPerformanceCounter + GetSystemTimeAsFileTime. The former is underlain by the most accurate hardware timer available on the PC. This varies (increasing in accuracy) as the age of the hardware lessens.

      Since circa 2005, all modern PCs have had a High Precision Event Time available as a part of the hardware. This is unaffected by clock speed energy management and lies outside of the number of cores or hyper-threads.

      (Am I too scary to talk to?)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      This article explains the limitations of the Time Stamp Counter register on multi-core x86 CPU's

      Which would be good information to post if Time::HiRes actually used the TSC!


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      So, is there way where i could program the code to run on only a single core?
        So, is there way where i could program the code to run on only a single core?

        Yes. See the /AFFINITY switch to the windows start command:

        AFFINITY The new application will have the specified processor affinity mask, expressed as a hexadecimal number.

        But I do not think that is the problem here. (As might be evidenced by your lack of response to Re^5: negative elapsed time? (versions?)?)


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: negative elapsed time?
by zentara (Archbishop) on Aug 08, 2011 at 14:58 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://919240]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-12-03 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (28 votes). Check out past polls.

    Notices?