Not a fair comparison. It's slow probably due to the $a = pdl @dat line,  and 
also you should see improvement if you just calc'd that 4th val as a separate 
pdl and catted it when you're finished. 

It's not fair since you are adding the time it takes to get the data into pdl 
from perl (which is a rare thing anyways). A fair comparison would be either 
generating the data directly in pdls, or reading from a file.

Even the field and pdl should kill perl by a large margin. 

-Judd

On Jul 9, 2010, at 12:27 AM, "P Kishor" <[email protected]> wrote:

> In my quest to learn PDL, I am trying out simple problems. Recently
> someone asked on Perlmonks the following question -- given a 3 x 100K
> array of arrays, how to 'push' a fourth value into each array
> calculated from the 1st and second elements. That is, given
> 
> [
>    [x1, y1, z1],
>    [x2, y2, z2]
>    ..
>    [x100K, y100K, z100K]
> ]
> 
> get
> 
> [
>    [x1, y1, z1, n1],
>    [x2, y2, z2, n2]
>    ..
>    [x100K, y100K, z100K, n100K]
> ]
> 
> where nn = (xn ** 2 + yn ** 2) ** 0.5
> 
> So, I piped in and suggested PDL. Given $a, a 3 x 100K piddle
> 
> # add a fourth column to hold the calculated values
> $b = $a->append( zeros(1, 100000) );
> 
> # calc the value
> $b->slice('3,:') .= (($b->slice('0,:') ** 2) + ($b->slice('1,:') ** 2)) ** 
> 0.5;
> 
> Then I was curious if PDL brought any speed advantage to this
> calculation. So, I wrote a little script
> 
> for my $c (10000, 100000, 1000000, 2000000, 3000000) {
> 
>    my @dat = ();
>    for (1 .. $c) {
>        push @dat, [int(rand(10)), int(rand(10)), int(rand(10))];
>    }
> 
>    by_pdl(\...@dat, $c);
>    #by_arr(\...@dat, $c);
> 
> }
> 
> 
> sub by_arr {
>    my $t0 = time();
> 
>    my ($dat, $count) = @_;
> 
>    for (@$dat) {
>        push @$_, (($_->[0] ** 2) + ($_->[1] ** 2)) ** 0.5;
>    }
> 
>    my $t1 = time();
> 
>    print "arr $count: " . ($t1 - $t0) . " secs\n";
> }
> 
> sub by_pdl {
>    my $t0 = time();
> 
>    my ($dat, $count) = @_;
> 
>    my $a = pdl @$dat;
> 
>    my $b = $a->append( zeros(1, $count) );
>    $b->slice('3,:') .= (($b->slice('0,:') ** 2) + ($b->slice('1,:')
> ** 2)) ** 0.5;
> 
>    my $t1 = time();
> 
>    print "pdl $count: " . ($t1 - $t0) . " secs\n";
> }
> 
> Here is what I got
> 
> punk...@lucknow ~$perl arr_v_pdl.pl
> arr   10000: 0.00679993629455566 secs
> arr  100000: 0.0664570331573486 secs
> arr 1000000: 0.673020124435425 secs
> arr 2000000: 1.33305907249451 secs
> arr 3000000: 2.01763200759888 secs
> 
> punk...@lucknow ~$perl arr_v_pdl.pl
> pdl   10000: 0.00865507125854492 secs
> pdl  100000: 0.102356910705566 secs
> pdl 1000000: 1.04948401451111 secs
> pdl 2000000: 2.03625702857971 secs
> pdl 3000000: 13.5644547939301 secs
> 
> As potentially embarrassing as it may be for me, I ask you -- am I
> doing something very wrong and silly? Otherwise please explain why PDL
> is so much slower than Perl.
> 
> -- 
> Puneet Kishor
> 
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to