Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Processing values of a piddle (PDL) speedup using 'at' vs. 'index'

by kevbot (Chaplain)
on Jul 18, 2012 at 04:37 UTC ( #982366=note: print w/ replies, xml ) Need Help??


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% --


Comment on Re^4: Processing values of a piddle (PDL) speedup using 'at' vs. 'index'
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://982366]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2015-07-07 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (90 votes), past polls