comatose
comatose

Reputation: 1962

Perl sleep and time measurement

I have this piece of code

my $time_start = time;
sleep 5;
my $time_diff = time - $time_start;
print "time elapsed: $time_diff";

The code mostly prints 5 as is expected, but sometimes it prints 4. What could possibily cause that? Is it the sleep which is the culprit, or is it the way of measuring time itself that can result in less precise values sometimes?

Upvotes: 1

Views: 1792

Answers (1)

fugu
fugu

Reputation: 6578

See perldoc -f sleep:

On some older systems, it may sleep up to a full second less than what you requested, depending on how it counts seconds. Most modern systems always sleep the full amount. They may appear to sleep longer than that, however, because your process might not be scheduled right away in a busy multitasking system.

Upvotes: 6

Related Questions