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

As we all know, Perl's core sleep() only likes integers. There are, however, many places where one would like to sleep for under a second, or for periods other than what can be expressed with normal integers.

There are a few different ways to do it, and the most common ways I've seen are Time::HiRes::sleep($n) or Time::HiRes::usleep($n * 1_000_000) (which, obviously, require Time::HiRes) and select(undef, undef, undef, $n).

Are the different ways of sleeping just a personal preference, or are there some real differences (Memory/CPU usage, accuracy, ...)?

And, finally, what do you use to sleep? Personally, I've always used Time::HiRes::sleep since it's what I used the first time I needed to sleep for a split-second, and the habit just stuck then. :)

Replies are listed 'Best First'.
Re: How do you sleep?
by gone2015 (Deacon) on Jan 27, 2009 at 15:36 UTC

    Given that Time::HiRes is a core module I think one can assume it is generally well supported, so using select(undef, undef, undef, $n) seems to me to be unnecessarily baroque.

    You can, of course, use Time::Hires qw(sleep) and magically transform all the sleep in the current package... Or go a stage further and:

    use Time::HiRes () ; BEGIN { *CORE::GLOBAL::time = \&Time::HiRes::time ; } ;
    and drag your entire program into the 21st Century, if you're feely lucky, modules-wise !

    However, the limitations of sleep are so well known, that overriding it will probably confuse -- so I favour a small utility module:

    package Htime ; use Exporter qw(import) ; our @EXPORT = qw(htime hsleep halarm) ; use Time::HiRes () ; *htime = \&Time::HiRes::time ; *hsleep = \&Time::HiRes::sleep ; *halarm = \&Time::HiRes::alarm ;
    ...and one can add a little code to check that a given minimum resolution is supported locally. It's then clear that where hsleep is used it isn't standard sleep -- and without all the Time::HiRes:: clutter.

Re: How do you sleep?
by JavaFan (Canon) on Jan 27, 2009 at 13:48 UTC
    I usually use 4-arg select, because the first time I needed to sleep for a split-second, Time::HiRes didn't exist yet.

    Besides, 4-arg select() works the same way it works in C.

      "Besides, 4-arg select() works the same way it works in C."

      I'm not exactly a C guru, so could you elaborate a bit on what you meant with that? Does select() behave differently on some situations than Time::HiRes::sleep or other alternatives (e.g. how signals are handled)?

        I just mean that it works the same way as in C - same arguments, same behaviour, nothing new to learn.

        I assume (but the documentation doesn't make it clear) that Time::HiRes::sleep suffers from the same problem as CORE::sleep(): on many platforms, sleep() is implemented using alarm().

OT: Re: How do you sleep?
by svenXY (Deacon) on Jan 27, 2009 at 13:37 UTC
    what do you use to sleep?

    usually, a good glass of red wine does the job fine ;-)

      I saw that one coming.. :)

      I was tempted to put in stuff about pillows and beds myself, but I'd rather see real replies this time. ;)