On Thursday 12 October 2006 23:52, John W. Krahn wrote:
> Write it in C.   :-)
Tempting, but I'm prototyping here. Perl is much faster to write than C, 
especially when dealing with complex data structures (for the more complex 
algorithms I'm playing with, some of them would be downright painful in C)

> Yes, Perl uses references which are conceptually similar to pointers.
yeah, I understand Perl references, but you can't (afaik) do C-style pointer 
arithmetic with them. Without pointer arithmetic, this access:
$details->[ $j ]
is something akin to (in C/Perl/psuedocode mishmash): 
address_of($details) + sizeof(void *) * $j
That multiply is happening 100 million times. I wonder if using foreach is any 
faster, I'd expect that it's optimised for skimming though arrays, and it may 
wind up faster even if I skip every second one perhaps. I guess DProf will 
tell me...

My original method (main::learn is the function I posted, I've flipped the 
lines around so they're always in the same order):
%Time ExclSec CumulS #Calls sec/call Csec/c  Name
 44.6   66.01 142.31      1   66.009 142.31  main::learn
 50.5   74.78 74.780 480189   0.0002 0.0002  main::get_user_details

Using a foreach with an 'if' to skip each second one (I didn't understand the 
$i ^= 1 trick until after I'd run this, don't know how much of a difference 
it'd make):
 55.6   91.53 161.97      1   91.539 161.97  main::learn
 41.9   68.90 68.909 480189   0.0001 0.0001  main::get_user_details

Paul's method where the operation is a one-liner using grep:
 41.9   52.99 122.67      1   52.999 122.67  main::learn
 53.8   68.11 68.119 480189   0.0001 0.0001  main::get_user_details

I think it's somewhat clear what the winner is here :)

>         my $users = get_all_users();
>         for ( my $i = 0; $i < @$users; ++$i ) {
>                 my $details = get_user_details( $users->[ $i ] );
>                 my $sum   = 0;
>                 my $count = 0;
>                 for ( my $j = 1; $j < @$details; $j += 2 ) {
>                         $sum += $details->[ $j ];
>                         $count++;
>                 }
>                 $means{ $user } = $sum / $count;
>         }
>
> Although in my tests that takes about the same time to execute but uses a
> *lot* less memory as it doesn't have to make a copy of the @$users array
> like your version does.
I don't follow. Where was my version copying @$users?

-- 
Robin <[EMAIL PROTECTED]> JabberID: <[EMAIL PROTECTED]>

Hostes alienigeni me abduxerunt. Qui annus est?

PGP Key 0xA99CEB6D = 5957 6D23 8B16 EFAB FEF8  7175 14D3 6485 A99C EB6D

Attachment: pgpNIjONqJ4PL.pgp
Description: PGP signature

Reply via email to