1#!/usr/bin/env perl 2 3# Profile Statistics::Descriptive::Full and Statistics::Descriptive::Discrete 4# for various data set sizes 5 6use strict; 7use warnings; 8 9use Statistics::Descriptive::Discrete; 10use Statistics::Descriptive; 11use Time::HiRes qw(gettimeofday tv_interval); 12use Math::Random qw(random_set_seed_from_phrase random_uniform_integer); 13use vars qw($MAXELEMENTS $NUMRUNS); 14 15$MAXELEMENTS = 1000000; #how big is the data set 16$NUMRUNS = 1; #how many times to run 17my $HIGH = 2**8; #sample range for data set. E.g. 2**8 = 256 possible values in data set 18 19my ($t0, $elapsed); 20 21my $stats_discrete = Statistics::Descriptive::Discrete->new(); 22my $stats_descr = Statistics::Descriptive::Full->new(); 23 24print "Statistics::Descriptive, runs = $NUMRUNS, elements=$MAXELEMENTS, sample range = $HIGH\n"; 25$t0 = [gettimeofday]; 26test_stats($stats_descr,1,$HIGH,$MAXELEMENTS,$NUMRUNS); #start at 1 so stats that divide by 0 will work 27$elapsed = tv_interval ( $t0, [gettimeofday]); 28print "Total time: $elapsed sec\n"; 29 30print "Statistics::Descriptive::Discrete, runs = $NUMRUNS, elements=$MAXELEMENTS, sample range = $HIGH\n"; 31$t0 = [gettimeofday]; 32test_stats($stats_discrete,1,$HIGH,$MAXELEMENTS,$NUMRUNS); #start at 1 so stats that divide by 0 will work 33$elapsed = tv_interval ( $t0, [gettimeofday]); 34print "Total time: $elapsed sec\n"; 35 36sub test_stats 37{ 38 my ($stats, $low, $high, $maxelements, $numruns) = @_; 39 40 foreach my $run (0..$numruns-1) 41 { 42 print "Run # $run\n"; 43 $stats->clear(); 44 random_set_seed_from_phrase("Test Statistics::Descriptive::Discrete"); 45 print "Adding data\n"; 46 foreach my $i (0..$maxelements-1) 47 { 48 my $randint = random_uniform_integer(1, $low, $high); 49 $stats->add_data($randint); 50 } 51 #compute and print out the stats 52 print "count = ",$stats->count(),"\n"; 53 #print "uniq = ",$stats->uniq(),"\n"; 54 print "sum = ",$stats->sum(),"\n"; 55 print "min = ",$stats->min(),"\n"; 56 print "min index = ",$stats->mindex(),"\n"; 57 print "max = ",$stats->max(),"\n"; 58 print "max index = ",$stats->maxdex(),"\n"; 59 print "mean = ",$stats->mean(),"\n"; 60 print "geometric mean = ",$stats->geometric_mean(),"\n"; 61 print "harmonic mean = ",$stats->harmonic_mean(),"\n"; 62 print "standard_deviation = ",$stats->standard_deviation(),"\n"; 63 print "variance = ",$stats->variance(),"\n"; 64 print "sample_range = ",$stats->sample_range(),"\n"; 65 print "mode = ",$stats->mode(),"\n"; 66 print "median = ",$stats->median(),"\n"; 67 my $f = $stats->frequency_distribution_ref(10); 68 foreach (sort {$a <=> $b} keys %$f) { 69 print "key = $_, count = $f->{$_}\n"; 70 } 71 } 72} 73 74