Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Simple question about sleep function

by Lucas Rey (Sexton)
on Sep 23, 2016 at 10:29 UTC ( [id://1172446]=perlquestion: print w/replies, xml ) Need Help??

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

Dear community, this is a very simple question, but I cannot found reply.

Which is the differences between:

sleep (1);

and

select(undef, undef, undef, 1);

It seems both stop the execution for 1 second. But, is there a real differences for example in term of performance?

Thank you. Lucas

Replies are listed 'Best First'.
Re: Simple question about sleep function
by haukex (Archbishop) on Sep 23, 2016 at 10:41 UTC

    Hi Lucas Rey,

    I believe one of the major differences is that select lets you specify subsecond precision for the sleep duration.

    My gut tells me I wouldn't expect a noticable difference between the two in terms of performance. But I'm confused why performance would matter for a function that effectively causes the script to hang?

    If you want higher precision, have a look at Time::HiRes.

    Regards,
    -- Hauke D

    Updated: Made wording a little more specific.

Re: Simple question about sleep function
by Discipulus (Canon) on Sep 23, 2016 at 10:51 UTC
    the four arg form of select allow to sleep for less than 1 second wich is the minimum for sleep

    iirc there were also something with block nonblock mode related to threads. or related to alarm (update: was this; see Alternatives to Mixing Alarm and Sleep).

    In most cases your snippets behave identically,though.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Simple question about sleep function
by hippo (Bishop) on Sep 23, 2016 at 11:02 UTC

    The main difference to me is that the purpose of the former is obvious for anybody having to maintain the code whereas the latter is much more obscure. If you have a good reason to use the latter, then comment it at every use. The former needs no comment (except for why you'd be sleeping in the first place, of course).

Re: Simple question about sleep function
by BrowserUk (Patriarch) on Sep 23, 2016 at 22:19 UTC

    Yes. sleep does whole (integer) seconds, whilst select takes a floating point value; and select is interruptible by signals, whilst sleep (at least on Win32) isn't.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
    Computers are making people easier to use everyday
Re: Simple question about sleep function
by Yary (Pilgrim) on Sep 23, 2016 at 22:07 UTC
    In addition to all the other good answers- "sleep(1)" is more portable than the "select(undef...)" call- something to consider when writing code that might be run elsewhere.

    And for portable sub-second waiting, there's Time::HiRes.

    The only reason to use the "select" version for pausing these days is for sub-second precision on embedded systems, where space is at a premium.

Re: Simple question about sleep function
by shmem (Chancellor) on Sep 24, 2016 at 17:06 UTC
    But, is there a real differences for example in term of performance?

    Of course there are differences, since they are different functions, and it all depends on how they are implemented which might differ between operating systems, but that's not something to be measured within perl as explained below.

    (The following applies to the Linux kernel 3.16.) On a Linux system, both sleep and select boil down to system calls, sleep being implemented via nanosleep(2) and select via select(2) (the 2 in parens indicate the manual page section, which is "system calls" on my system.)

    With both calls, perl notifies the kernel of a timeout for the function, after which program execution should continue, so performance differences depend on the respective kernel implementations. Since select(2) takes more arguments (e.g. filehandles) than, the kernel might allocate more resources for this specific call than nanosleep(2), but the call could internally be optimized to nanosleep(2) - I don't know and won't look.

    As others pointed out, perl's sleep (for compatibility reasons?) takes only the integer part of a decimal number, and negative values for sleep are wrapped around to positive values. Absolute values below 0 (e.g. -0.2345) are considered 0 and the call is then optimized away.

    qwurx [shmem] ~> strace -e trace=nanosleep perl -e 'sleep 1' nanosleep({1, 0}, 0x7ffe9dc8df90) = 0 +++ exited with 0 +++ qwurx [shmem] ~> strace -e trace=nanosleep perl -e 'sleep 0.123456' +++ exited with 0 +++ qwurx [shmem] ~> strace -e trace=nanosleep perl -e 'sleep -1' nanosleep({4294967295, 0}, ^CProcess 27923 detached <detached ...> qwurx [shmem] ~> strace -e trace=nanosleep perl -e 'sleep 3.1415926' nanosleep({3, 0}, 0x7ffed4b7b6a0) = 0 +++ exited with 0 +++ qwurx [shmem] ~> strace -e trace=select perl -e 'select $u,$u,$u, 3.14 +15926' select(0, NULL, NULL, NULL, {3, 141592}) = 0 (Timeout) +++ exited with 0 +++ qwurx [shmem] ~> strace -e trace=select perl -e 'select $u,$u,$u, -3.1 +415926' select(0, NULL, NULL, NULL, {0, 0}) = 0 (Timeout) +++ exited with 0 +++

    The timeout argument of select(2) is struct timeval, which on my system is

    struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t tv_usec; /* Microseconds. */ };

    so select(2) can be called with fractions of a second as timeout. Values below zero, however, are just set to 0.

    In summary, for any difference of perfomance to be measured within perl, one would have to emit huge amounts of either system call to get some difference, which for sleep implies interruption of each system call for the test to terminate before you grow old; and even then, you would just measure the kernel's performance for each call from perl's point of view. The results probably would be misleading, since the kernel has other things to do than providing timeouts for a benchmark script; and there are other tools to profile the kernel.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Simple question about sleep function
by Lucas Rey (Sexton) on Sep 24, 2016 at 04:54 UTC
    Thank you all!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found