You could also try "fudging" your way to the next second.
#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11111868
use warnings;
use Time::HiRes qw( time sleep );
my $fudgefactor = 0.01;
while(1)
{
my $nextsecond = int time + 1;
sleep $nextsecond - time - $fudgefactor;
1 while time < $nextsecond;
printf "%.6f\n", time;
}
Outputs:
1579972033.000003
1579972034.000004
1579972035.000004
1579972036.000004
1579972037.000004
1579972038.000005
1579972039.000003
1579972040.000004
1579972041.000004
1579972042.000003
1579972043.000003
1579972044.000004
1579972045.000003
1579972046.000005
1579972047.000003
Looks fairly consistent :)