in reply to Re^3: Processing values of a piddle (PDL) speedup using 'at' vs. 'index'
in thread Processing values of a piddle (PDL) speedup using 'at' vs. 'index'
I agree (but my actual use case is a little more complex than the code examples that I have given here). I added an additional subroutine to my benchmark code to see how much faster your suggested approach would be. In the 'perl_array' sub, I export the entire pdl to a perl array using the PDL 'list' command. Then, I search the perl array for the "special value" and replace them with an empty string. This is approach is faster.
#!/usr/bin/env perl use strict; use warnings; use PDL; use Benchmark qw(:all); cmpthese( 500, { 'pdl_values' => sub {&pdl_value}, 'perl_values' => sub {&perl_scalar_value}, 'perl_array' => sub {&perl_array}, } ); exit; sub pdl_value { my $pdl = ones( float, 10000 ); $pdl->index(0) .= 999; my $nelem = nelem($pdl); my $special_value = 999; my @values; for ( my $i = 0; $i < $nelem; ++$i ) { my $val = $pdl->index($i); if ( $val == $special_value ) { $val = undef; } push @values, $val; } #Do something with @values return; } sub perl_scalar_value { my $pdl = ones( float, 10000 ); $pdl->index(0) .= 999; my $nelem = nelem($pdl); my $special_value = 999; my @values; for ( my $i = 0; $i < $nelem; ++$i ) { my $val = $pdl->at($i); if ( $val == $special_value ) { $val = undef; } push @values, $val; } #Do something with @values return; } sub perl_array { my $pdl = ones( float, 10000 ); $pdl->index(0) .= 999; my $nelem = nelem($pdl); my $special_value = 999; my @perl_array = list $pdl; my @values = map { $_ == $special_value ? '' : $_ } @perl_array; #Do something with @values return; }
Rate pdl_values perl_values perl_array pdl_values 1.52/s -- -97% -99% perl_values 52.3/s 3345% -- -73% perl_array 197/s 12865% 276% --
|
|---|