Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created July 26, 2019 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosimo/3f4569b4ef67b673996eff1cfc266eb6 to your computer and use it in GitHub Desktop.
Save cosimo/3f4569b4ef67b673996eff1cfc266eb6 to your computer and use it in GitHub Desktop.
Calculate the basic statistical attributes of a numeric population
#!/usr/bin/env perl
=head1 NAME
quick-stats.pl - Calculate the basic statistical attributes of a list of numbers
=head1 SYNOPSIS
echo <list of numbers> | ./quick-stats.pl
=head1 DESCRIPTION
Calculate the basic statistical attributes of a numeric population.
Used for example when trying to troubleshoot response times. Extracting just
the response times from arbitrary log files and piping them to this script
gives you a good picture of how the response time variable is distributed,
what is the maximum value seen in the input, and what's the standard deviation.
=cut
use 5.014;
use strict;
use open qw( :encoding(UTF-8) :std );
use utf8;
use warnings qw( FATAL utf8 );
use autodie;
my $avg;
my $max;
my $min;
my @samples;
my $total = 0;
my $stddev = 0;
while (readline) {
chomp;
my $n = 0 + $_;
if (! defined $min || $min > $n) { $min = $n }
if (! defined $max || $max < $n) { $max = $n }
$total += $n;
push @samples, $n;
}
$avg = $total / @samples;
for my $n (@samples) {
$stddev += ($n - $avg) ** 2;
}
$stddev = sqrt($stddev / @samples);
say "min: $min";
say "max: $max";
say "average: $avg";
say "stddev: $stddev";
say "total: $total";
say "samples: ", scalar(@samples);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment