Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: The most precise second (timer)

by duelafn (Parson)
on Nov 26, 2019 at 15:22 UTC ( [id://11109266]=note: print w/replies, xml ) Need Help??


in reply to The most precise second (timer)

Just save the value and test before sleeping.

use strict; use warnings; use Time::HiRes qw/time sleep/; sub negative_timer { my $max = $_[0]; my @time_test; my $start = time(); for (my $i = 0; $i < $max; $i++) { $start += 1; my $dt = $start - time(); if ($dt > 0) { sleep $dt; $time_test[$i] = time(); } } return @time_test; }

Good Day,
    Dean

Replies are listed 'Best First'.
Re^2: The most precise second (timer)
by tukusejssirs (Beadle) on Nov 26, 2019 at 15:29 UTC

    Thanks, Dean! I thought about doing that, but that would slow the calculation a bit, and thus the calculated second would differ a bit more.

    Isn’t there a way to accomplish this by addition (incrementing the time by a second) instead of substraction (decreamenting the time by a second)?

    Update

    As I have said, after some tests, the average difference from a real second is -0.000003595544834, which is about 0.000003513663706 more than the average difference in the original code.

    I know this is still somewhat irrelevant difference, but we need as precise second as we can get, and since the original version of the code is more precise (by that much), this new code does not solve our problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found