Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by kevbot (Hermit)
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 romping around the Monastery: (10)
As of 2014-10-23 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (126 votes), past polls