On Fri, Jul 9, 2010 at 12:42 AM, Judd Taylor <[email protected]> wrote:
> 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.

right you are. I took the line that builds the piddle out of the
timing loop and I got an improvement.

punk...@lucknow ~$perl arr_v_pdl.pl
pdl 10000: 0.00195884704589844 secs
pdl 100000: 0.0168581008911133 secs
pdl 1000000: 0.172031164169312 secs
pdl 2000000: 0.34131908416748 secs
pdl 3000000: 1.84429693222046 secs
punk...@lucknow ~$perl arr_v_pdl.pl
arr 10000: 0.00655102729797363 secs
arr 100000: 0.0652320384979248 secs
arr 1000000: 0.660249948501587 secs
arr 2000000: 1.32096791267395 secs
arr 3000000: 1.98908710479736 secs

But, not a *huge* improvement, particularly for very large n. Maybe it
is just noise, but I did repeat the test a few times.

By the way, I don't know how to cat a 3 x 100K piddle to a 1 x 100K
piddle to get a 4 x 100K piddle, so I did an append. From cat's docs,
it seems I need N piddles of same dimensions to result in an N+1
piddle... that is not the case here.

In any case, back to the speed -- it seems the PDL starts croaking at
very large numbers, perhaps because of memory management? For example,
if I increase n to 4000000, my computer hangs for a longish time.


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



-- 
Puneet Kishor http://www.punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
-----------------------------------------------------------------------
Assertions are politics; backing up assertions with evidence is science
=======================================================================

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

Reply via email to