How do I sleep for a millisecond in Perl?
Asked Answered
O

7

86

How do I sleep for shorter than a second in Perl?

Outrun answered 22/5, 2009 at 8:38 Comment(1)
It is because I believe this title is actually more helpful. Let me try to argument this. What B.D.F. did was duplicate the question in the title which adds no expressivity. When I was searching the web for the answer I actually looked for a millisecond sleep and not for a "shorter than a second" sleep. I say this version has a better chance of being hit through different google searches than B.D.F's one. Please correct me if I'm wrong.Aaberg
N
121

From the Perldoc page on sleep:

For delays of finer granularity than one second, the Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) provides usleep().

Actually, it provides usleep() (which sleeps in microseconds) and nanosleep() (which sleeps in nanoseconds). You may want usleep(), which should let you deal with easier numbers. 1 millisecond sleep (using each):

use strict;
use warnings;

use Time::HiRes qw(usleep nanosleep);

# 1 millisecond == 1000 microseconds
usleep(1000);
# 1 microsecond == 1000 nanoseconds
nanosleep(1000000);

If you don't want to (or can't) load a module to do this, you may also be able to use the built-in select() function:

# Sleep for 250 milliseconds
select(undef, undef, undef, 0.25);
Nightwear answered 22/5, 2009 at 8:44 Comment(3)
Caveat: although you could nanosleep(1), many OSes have some minimum granularity; e.g. I believe it's 1 ms in Windows XP, so you'd still sleep for a whole 1000000 ns there.Fir
The page on Time::HiRes says something like "if you need nanosleep(), you should reconsider whether or not Perl is the best tool for your job."Nightwear
There is also Time::HiRes::sleep( 0.3 ) which will sleep for .3 of a second.Radiotelephony
B
39

Time::HiRes:

  use Time::HiRes;
  Time::HiRes::sleep(0.1); #.1 seconds
  Time::HiRes::usleep(1); # 1 microsecond.

http://perldoc.perl.org/Time/HiRes.html

Berne answered 22/5, 2009 at 8:43 Comment(0)
R
14

From perlfaq8:


How can I sleep() or alarm() for under a second?

If you want finer granularity than the 1 second that the sleep() function provides, the easiest way is to use the select() function as documented in select in perlfunc. Try the Time::HiRes and the BSD::Itimer modules (available from CPAN, and starting from Perl 5.8 Time::HiRes is part of the standard distribution).

Rosetterosewall answered 22/5, 2009 at 11:21 Comment(0)
U
7

Use Time::HiRes.

Unfamiliar answered 22/5, 2009 at 8:44 Comment(0)
D
5

A quick googling on "perl high resolution timers" gave a reference to Time::HiRes. Maybe that it what you want.

Dipteral answered 22/5, 2009 at 8:44 Comment(0)
G
2
system "sleep 0.1";

does the trick.

Gaeta answered 23/2, 2017 at 23:3 Comment(3)
It actually worked for me (not elegant, but for debug purposes it seems to work).Kyles
not really perl, but helpfull if you don't want to install Time::HiRes but need some sleeps smaller than 1 second but don't forget, that spawning a shell for this takes time itself, so you can't count on accuracy!Forecourt
works on linux and many systems with modern GNU sleep utillity installed, but will fail on BSD, MacOS, busybox-Linux, etc. (Actually, it may work on BSD/MacOS depending on which sleep is part of the distribution)Carcanet
C
0

For 1-liner on the CLI (for systems that don't have GNU's sleep:

perl -MTime::HiRes=usleep -e 'usleep(1000000);';

will sleep 1s. You can verify this by prefixing time to the command. So to convert from microseconds to milliseconds, multiply your expected milliseconds by 1000.

~ otheus$ time perl -MTime::HiRes=usleep -e 'usleep(1000000);';

real  0m1.032s
user  0m0.011s
sys   0m0.010s
~ otheus$ time perl -MTime::HiRes=usleep -e 'usleep(500000);';

real  0m0.547s
user  0m0.010s
sys   0m0.026s
Carcanet answered 2/12, 2023 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.