Subject: | [Patch] Speed up skewness calculations |
Hello Shlomi,
The attached patch does some minor refactoring on the skewness calculation to avoid needless variable creation. Kurtosis already uses this approach so it also makes the code more consistent.
Benchmarking indicates it is typically 30-35% faster.
Regards,
Shawn.
Subject: | skewness.patch |
diff -r 002168f13d13 Statistics-Descriptive/lib/Statistics/Descriptive.pm
--- a/Statistics-Descriptive/lib/Statistics/Descriptive.pm Sat Feb 01 10:42:25 2014 +0200
+++ b/Statistics-Descriptive/lib/Statistics/Descriptive.pm Fri Jan 09 15:41:31 2015 +1100
@@ -823,11 +823,10 @@
my $mean = $self->mean();
my $sum_pow3;
+ foreach my $rec ( $self->get_data ) {
+ $sum_pow3 += (($rec - $mean) / $sd) ** 3;
+ }
- foreach my $rec ( $self->get_data ) {
- my $value = (($rec - $mean) / $sd);
- $sum_pow3 += $value ** 3;
- }
my $correction = $n / ( ($n-1) * ($n-2) );
@@ -848,7 +847,7 @@
my $kurt;
my $n = $self->count();
- my $sd = $self->standard_deviation();
+ my $sd = $self->standard_deviation();
if ( $sd && $n > 3) {