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).
| [reply] [Watch: Dir/Any] |
Re: How to sleep 1 microsecond?
by GrandFather (Saint) 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
| [reply] [Watch: Dir/Any] |
|
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...
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: How to sleep 1 microsecond?
by lidden (Curate) 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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
#!/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]
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
You're totally right, sorry. I just checked Google!
| [reply] [Watch: Dir/Any] |
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);? | [reply] [Watch: Dir/Any] [d/l] |
|
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? ;)
| [reply] [Watch: Dir/Any] |
|
True. I won't get a nanosecond of sleep until this mystery is resolved.
| [reply] [Watch: Dir/Any] |
Re: How to sleep 1 microsecond?
by tobyink (Canon) 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'
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: How to sleep 1 microsecond?
by Lotus1 (Vicar) 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.
| [reply] [Watch: Dir/Any] |
Re: How to sleep 1 microsecond?
by sundialsvc4 (Abbot) 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.
| [reply] [Watch: Dir/Any] |