in reply to Live operations-per-second counter

I like the alarm idea. But using threads would be even better.
use strict; use Time::HiRes qw ( alarm time usleep sleep); our $starttime; our $lasttime; our $linecount = 0; our $lastlinecount = 0; $SIG{ALRM} = sub { my $now = time; printf "\rline %d %8.1f avg lines per second %8.1f lines per s +econd", $linecount, $linecount/($now - $starttime), ($linecount - $lastlinecount) / ($now - $lasttime); $lasttime = time(); $lastlinecount = $linecount; }; alarm(1, .5); $| = 1; $lasttime = $starttime = time(); while (1) { $linecount++; last if $linecount > 9999; usleep(0.001); }; print "\n"; alarm(0); $SIG{ALRM} = undef;
-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re^2: Live operations-per-second counter
by gam3 (Curate) on Mar 21, 2005 at 18:33 UTC
    And here is that threaded solution.
    use strict; use Config; $Config{useithreads} or die "Recompile Perl with threads to run this p +rogram."; use threads; use threads::shared; use Time::HiRes qw ( time sleep); our $starttime : shared; our $linecount : shared = 0; our $running : shared = 1; sub progress { my $lasttime = $starttime; my $lastlinecount = 0; sleep(1); $| = 1; do { my $now = time(); printf "\rline %d %8.1f avg lines per second %8.1f lines per s +econd", $linecount, $linecount/($now - $starttime), ($linecount - $la +stlinecount) / ($now - $lasttime); $lasttime = time(); $lastlinecount = $linecount; sleep(.5); } while ($running); }; $starttime = time(); our $thr1 = threads->new(\&progress); while (1) { $linecount++; last if $linecount > 9999; sleep(0.001); }; print "\n"; $running = 0; $thr1->join();
    -- gam3
    A picture is worth a thousand words, but takes 200K.