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

How to sleep 1 microsecond?

by sophate (Beadle)
on Aug 17, 2012 at 07:37 UTC ( #987928=perlquestion: print w/ replies, xml ) Need Help??
sophate has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm writing some codes that need to sleep for a very short time, say 1 microsecond. I tried "Time-HiRes" and used "usleep" or "nanosleep". However, usleep and nanosleep actually sleep much longer than 1 microsecond. From the returned value of the function calls, they sleep about 1 milisecond(1000 microseconds) even though I specify the sleep interval to 1 microsecond. Here is my program. Any suggestions how I can force my Perl program to sleep about 1 microsecond? Thanks in advance.

#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(usleep nanosleep gettimeofday); my @start = gettimeofday (); my $sleep = usleep(1); my @end = gettimeofday (); print "[$sleep][@start][@end]\n"; --------------------------------------- Sample Results: [805][1345188496 560216][1345188496 561029]

Comment on How to sleep 1 microsecond?
Download Code
Re: How to sleep 1 microsecond?
by influx (Beadle) on Aug 17, 2012 at 07:57 UTC
    Did you actually try nanosleep(1000000); ? I didn't see that in your sample.

      One microsecond should be 1000 nanoseconds.


      Dave

        Hi, the sample codes above use usleep. Here is the program using nanosleep.

        #!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(usleep nanosleep gettimeofday); my @start = gettimeofday (); my $sleep = nanosleep(1000); my @end = gettimeofday (); print "[$sleep][@start][@end]\n"; Results: [1000][1345190945 846575][1345190945 846739]

        Compared with the codes that just get the timestamps two times(without the nanosleep line). The nanosleep actually sleeps much longer than 1 microsecond.

        #!/usr/bin/perl use strict; use warnings; use Time::HiRes qw(usleep nanosleep gettimeofday); my @start = gettimeofday (); my @end = gettimeofday (); print "[@start][@end]\n"; Results: [1345190988 610721][1345190988 610723]
        You're totally right, sorry. I just checked Google!
Re: How to sleep 1 microsecond?
by GrandFather (Cardinal) on Aug 17, 2012 at 08:06 UTC

    What are you trying to achieve with a 1us sleep? Even if you do find code that can achieve that some of the time, it will be highly unreliable for most common operating systems.

    True laziness is hard work

      Not to mention, the various sleep functions only guarantee that they sleep at least the amount of time specified. You can't really do accurate timing with them.

      Time::HiRes::ualarm might work better though...

        Thanks but the ualarm function is like usleep which is not reliable if I want to count a few microsecond.

      Concur that usleep/nanosleep are not reliable in this case :-(

Re: How to sleep 1 microsecond?
by moritz (Cardinal) on Aug 17, 2012 at 08:12 UTC

    If you are on a modern operating system, you cannot do that reliably. Any kind of sleep call is likely to trigger a task switch where the OS puts your process in the background executes another one. And even if you don't sleep, that can still happen.

    If you absolutely need such exact timings (what for?) consider using a real-time operating system, and probably not Perl either (Perl does some stuff under the hood that can be harmful when you need exact timings. Examples are re-hashing of hash tables, and freeing variables on scope exit).

Re: How to sleep 1 microsecond?
by influx (Beadle) on Aug 17, 2012 at 08:31 UTC
    What about select(undef, undef, undef,  0.000001); ?

      It won't be any more accurate than the Time::HiRes::nanosleep function.

      I've been sitting here for an hour hoping that our esteemed brother would share with us what he's using single microsecond granularity for. How am I supposed to get any sleep tonight with this mystery unresolved? ;)


      Dave

        True. I won't get a nanosecond of sleep until this mystery is resolved.
Re: How to sleep 1 microsecond?
by sundialsvc4 (Monsignor) on Aug 17, 2012 at 12:38 UTC

    “Briefly,” any sleep-call is a voluntary giving-up of control to some other process or thread, for not less than the amount of time specified.   If you are waiting to find a way to wait for 1 microsecond, you will be waiting for Godot.

Re: How to sleep 1 microsecond?
by lidden (Deacon) on Aug 17, 2012 at 13:15 UTC
    You cannot use gettimeofday to measure how long you sleep since a call to that function by itself takes too long for your needs.
    my @first = gettimeofday; my @second = gettimeofday; say "[@first] [@second] ", $second[1]-$first[1];
    Output:
    [1345208919 179070] [1345208919 179072] 2
Re: How to sleep 1 microsecond?
by tobyink (Abbot) on Aug 17, 2012 at 16:36 UTC
    use Time::HiRes qw(time); my $until = time + 0.00_001; 1 while time < $until;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thanks. This works pretty well :-) But just wondering why the smallest unit for time() is 0.00_001 but not 0.000_001?

        Meh, did I only do a one-hundred-thousandth of a second? Always getting my decimal places mixed up! Use whatever fraction of a second you like, but be aware of the limits of floating point numbers.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: How to sleep 1 microsecond?
by Lotus1 (Chaplain) on Aug 17, 2012 at 18:07 UTC

    I read somewhere that Salvadore Dali would fall asleep holding a spoon above a pan so it would wake him. Scientists call this microsleep.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-08-21 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (134 votes), past polls