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'

In reply to Re: Simple question about sleep function by shmem
in thread Simple question about sleep function by Lucas Rey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.