in reply to Live operations-per-second counter

This is the simple solution I've implemented, following Tanktalus's advice:
my $stop; my $speed = 0; my $start = time; while (<CONNS>) { $counter++; if ($counter =~ /00$/) { $stop = time; my $diff = $stop - $start; my $tdiff = ($diff == 0) ? 1 : $diff; $speed = sprintf("%.f", ($counter/$tdiff)); } ... line processing ... print "Processed: $counter (at $speed lines/s) \r"; }
Not so elegant or fast, but works. Thanks everyone!

Replies are listed 'Best First'.
Re^2: Live operations-per-second counter
by ikegami (Patriarch) on Mar 20, 2005 at 22:46 UTC
    if ($counter =~ /00$/) {
    can be written as
    if ($counter % 100 == 0) {
    which saves perl from converting the number to a string and checking it against a regexp.
      if ($counter % 100 == 0) {
      Or even
      unless ($counter % 100) {
      (But then it depends on how much one is familiar with such a construct - for me it is quite as intuitive as the former, but it's obvious that others' mileage may vary)