This has been delayed because of a) the need for a real-world example to diffuse the possibility of critisism for using an artifical example; b) the difficulty in actually making the translated code work.
Starting with the full solution to Re^4: Restore the original order of an array after sort and performing some funchtion on array values, and using a dataset size per Re^6: Restore the original order of an array after sort and performing some funchtion on array values,
this code 4000: Took 1.369 seconds:
Standard Perl:
</reamore>#! perl -slw use strict; use Time::HiRes qw[ time ]; use List::Util qw[ min ]; use Data::Dump qw[ pp ]; srand 1; our $N //= 4000; my $start = time(); my %pvalues = map{ $_ => rand() } 1 .. $N; #my %pvalues = ( # 1=> 0.5453980, # 2=> 0.4902384, # 3=> 0.8167950, # 4=> 0.2821822, # 5=> 0.4693030, # 6=> 0.6491767, # 7=> 0.9802138, # 8=> 0.1155778, # 9=> 0.9585124, # 10=> 0.4069490 #); my @orderedKeys = sort { $pvalues{ $b } <=> $pvalues{ $a } } keys %pvalues; my $d = my $n = values %pvalues; $pvalues{ $_ } *= $n / $d-- for @orderedKeys; $pvalues{ $orderedKeys[ $_ ] } = min( @pvalues{ @orderedKeys[ 0 .. $_ ] } ) for 1 .. $n-1; $pvalues{ $_ } = min( $pvalues{ $_ }, 1 ) for keys %pvalues; printf "$N: Took %.3f seconds", time() - $start;
Converted to use autobox::Core,
it requires 4000: Took 237.630 seconds:
(Partially) autoboxed code:
#! perl -slw use strict; use autobox::Core; use Time::HiRes qw[ time ]; use List::Util qw[ min ]; use Data::Dump qw[ pp ]; srand 1; our $N //= 4000; my $start = time(); my %pvalues = @{ [ 1 .. $N ]->map( sub{ $_[0] => rand() } ) }; #our %pvalues = ( # 1=> 0.5453980, # 2=> 0.4902384, # 3=> 0.8167950, # 4=> 0.2821822, # 5=> 0.4693030, # 6=> 0.6491767, # 7=> 0.9802138, # 8=> 0.1155778, # 9=> 0.9585124, # 10=> 0.4069490 #); my @orderedKeys = @{ %pvalues->keys->sort( sub{ $pvalues{ $_[1] } <=> $pvalues{ $_[0] } } ) }; my $d = my $n = @{ %pvalues->keys }; @orderedKeys->map( sub{ $pvalues{ $_[0] } *= $n / $d--; } ); [ 1 .. $n-1 ]->map( sub{ $pvalues{ $orderedKeys[ $_[0] ] } = min( @pvalues{ @orderedKeys[ 0 .. $_[0] ] } ); } ); %pvalues->keys->map( sub{ $pvalues{ $_[0] } = min( $pvalues{ $_[0] }, 1 ) } ); printf "$N: Took %.3f seconds\n", time() - $start; #pp \%pvalues;
I get that to be a 99.5% slowdown.
Update: It was pointed out that the above figure is deceptive, in that what it shows is that the standard perl version took just 0.5% of the time that the autoboxed version took. Looked at the other way around, the autoboxed version took 17000% longer.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: autobox performance:a real-world comparison
by chromatic (Archbishop) on Mar 05, 2010 at 05:02 UTC | |
by BrowserUk (Patriarch) on Mar 05, 2010 at 05:36 UTC | |
by karavelov (Monk) on Mar 05, 2010 at 10:21 UTC | |
by BrowserUk (Patriarch) on Mar 07, 2010 at 04:40 UTC | |
by BrowserUk (Patriarch) on Mar 07, 2010 at 07:13 UTC | |
|
Re: autobox performance:a real-world comparison
by JavaFan (Canon) on Mar 05, 2010 at 11:41 UTC | |
|
Re: autobox performance:a real-world comparison
by syphilis (Archbishop) on Mar 07, 2010 at 02:23 UTC | |
by BrowserUk (Patriarch) on Mar 07, 2010 at 04:26 UTC |