Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: $str to %hash to @ary

by Limbic~Region (Chancellor)
on Jul 25, 2004 at 13:27 UTC ( [id://377268]=note: print w/replies, xml ) Need Help??


in reply to Re^4: $str to %hash to @ary
in thread $str to %hash to @ary

fizbin,
I knew I was trading speed in setup time for speed in lookup time. If you want to see me on top, change the following:
"L~R" => sub { my @foo; my $str = "17:43:33:21:23:19:27:6"; while (my $p = $str =~ /([^:]+)/g && $1 and my $add_id = $str +=~ /([^:]+)/g && $1) { push @foo, ($add_id) x $p; } \@foo; }, "L~R" => sub { my $addid = $_[0]->[ rand 100 ]; }, As mod_perl: (NOT from scratch each time): Rate fizbin ccn_fast ccn ccn_faster origCode du +ff L~R fizbin 35680/s -- -52% -53% -60% -91% -9 +1% -92% ccn_fast 74642/s 109% -- -2% -16% -80% -8 +1% -83% ccn 76321/s 114% 2% -- -14% -80% -8 +1% -82% ccn_faster 88368/s 148% 18% 16% -- -76% -7 +8% -80% origCode 375784/s 953% 403% 392% 325% -- - +6% -13% duff 398928/s 1018% 434% 423% 351% 6% +-- -8% L~R 432419/s 1112% 479% 467% 389% 15% +8% --
I did notice that not on every pass all methods were "passing". If someone wanted to make it even faster they would randomize the array as part of set up time and then just loop through the array.

Cheers - L~R

Replies are listed 'Best First'.
Re^6: $str to %hash to @ary
by fizbin (Chaplain) on Jul 25, 2004 at 14:51 UTC
    I still want to know why there's the consistent time difference among the top three each time in the mod_perl case - it's the same code on the lookup end, once you drop the unnecessary call to int that's in the methods duff and origCode.

    I'll note that your fast version is essentially what duff did, except with regexes to extract the array instead of split followed by splice.

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      I guess this happens because duff's code have properly ordered array of probabilities, but origCode does not.

      UPDATE: yes I am right

      my @array = () x 100; cmpthese( 10000000, {head => sub{$array[0]}, tail => sub{$array[99]}} ); Rate tail head tail 4992511/s -- -3% head 5149331/s 3% --

      UPDATE: no, I am wrong. In both the cases the order doesn't matter, sorry

        But what does matter is how the arrays were made:
        my $str = shift || "17:43:33:21:23:19:27:6"; my @a1 = (); my @a2 = (); my @a = split /:/, $str; while ( my ( $p, $ad ) = splice @a, 0, 2 ) { push @a1, map {$ad} (1..$p); push @a2, ($ad) x $p; } use Benchmark 'cmpthese'; print "Accessing element 0:\n"; cmpthese(-1, { made_by_map => sub { $a1[0]; }, made_by_x => sub { $a2[0]; } }); print "\nAccessing element 50:\n"; cmpthese(-1, { made_by_map => sub { $a1[50]; }, made_by_x => sub { $a2[50]; } }); print "\nAccessing element 99:\n"; cmpthese(-1, { made_by_map => sub { $a1[99]; }, made_by_x => sub { $a2[99]; } }); print "\nAccessing random element:\n"; cmpthese(-1, { made_by_map => sub { $a1[rand 100]; }, made_by_x => sub { $a2[rand 100]; } });
        Using the same string as in the problem (i.e. with no argument passed), we get:
        Accessing element 0: Rate made_by_x made_by_map made_by_x 7554581/s -- -11% made_by_map 8479758/s 12% -- Accessing element 50: Rate made_by_x made_by_map made_by_x 7788171/s -- -4% made_by_map 8095273/s 4% -- Accessing element 99: Rate made_by_map made_by_x made_by_map 8058884/s -- -9% made_by_x 8882975/s 10% -- Accessing random element: Rate made_by_map made_by_x made_by_map 1724352/s -- -2% made_by_x 1758653/s 2% --
        In other words, the array made by map is faster accessing elements close the front, but slower accessing elements near the end (enough so that the random element access test goes in favor of the list created by x)

        However, the results for different initial strings aren't really making any sense - when I use a string of one hundred repetitions of "1:8", I get essentially opposite results: the x array is significantly faster at doing [0], (by ~ 20%) there's a tie for [50] and the map array wins big at doing [99] (by ~ 10%), and then map wins at the random index test.

        So far so good, but when I use "100:8", I get the map array winning at [50] and [99] and only losing by 1% on [0], yet still losing the random element test by 2%.

        Are all my results just noise? I think I should make this a new SoPW post.

        -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found