Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Premature and micro optimization...

by shmem (Chancellor)
on Jun 11, 2007 at 08:39 UTC ( #620441=note: print w/replies, xml ) Need Help??


in reply to Premature and micro optimization...

Seems like you messed up your benchmarks, and your results might be bogus therefore ;-)

There's a sub missing; adapting your benchmark code to what you wrote, with some subs added

#!/usr/bin/perl use strict; use warnings; use Benchmark qw/:all/; my $TIMES = 500000; my $upper_id_string = "ABCD1EFGHI2J.a01f2345b067cde890f12gab345678c9"; my $lower_id_string = "abcd1efghi2j.a01f2345b067cde890f12gab345678c9"; sub only_tr { my $string = shift; my( $id, $session ) = split( /\./, $string, 2 ); $id =~ tr/a-z/A-Z/; return( $id, $session ); } sub match_and_tr { my $string = shift; my( $id, $session ) = split( /\./, $string, 2 ); $id =~ /[a-z]/ and $id =~ tr/a-z/A-Z/; return( $id, $session ); } sub old { my $string = shift; return split( /\./, $string, 2 ); } sub new { my $string = shift; my( $id, $session ) = split( /\./, $string, 2 ); return( $id, $session ); } sub short { split( /\./, $_[0],2 ); } sub short_lc { split( /\./, lc($_[0]),2 ); } my $only_tr_low = sub { my( $id, $session ) = only_tr( $lower_id_string ) }; my $only_tr_upp = sub { my( $id, $session ) = only_tr( $upper_id_string ) }; my $match_tr_low = sub { my( $id, $session ) = match_and_tr( $lower_id_string ) }; my $match_tr_upp = sub { my( $id, $session ) = match_and_tr( $upper_id_string ) }; my $old_low = sub { my( $id, $session ) = old( $lower_id_string ) }; my $old_upp = sub { my( $id, $session ) = old( $upper_id_string ) }; my $new_low = sub { my( $id, $session ) = new( $lower_id_string ) }; my $new_upp = sub { my( $id, $session ) = new( $upper_id_string ) }; my $short_low = sub { my( $id, $session ) = short( $lower_id_string ) }; my $short_upp = sub { my( $id, $session ) = short( $upper_id_string ) }; my $short_lc_low = sub { my( $id, $session ) = short_lc( $lower_id_string ) }; my $short_lc_upp = sub { my( $id, $session ) = short_lc( $upper_id_string ) }; cmpthese( $TIMES, { 'Old way L' => $old_low, 'New way L' => $new_low, 'Old way U' => $old_upp, 'New way U' => $new_upp, 'Short L' => $short_lc_low, 'Short U' => $short_lc_upp, } ); print "\n"; cmpthese( $TIMES, { 'Match TR LC' => $match_tr_low, 'Match TR UC' => $match_tr_upp, 'Only TR LC' => $only_tr_low, 'Only TR UC' => $only_tr_upp, 'Old way L' => $old_low, 'Old way U' => $old_upp, 'New way L' => $new_low, 'New way U' => $new_upp, 'Short lc L' => $short_lc_low, 'Short lc U' => $short_lc_upp, 'Short way L' => $short_low, 'Short way U' => $short_upp, }, );

I get different results -

Rate New way U New way L Old way U Old way L Short U S +hort L New way U 227273/s -- -14% -35% -36% -38% + -38% New way L 264550/s 16% -- -24% -25% -28% + -28% Old way U 349650/s 54% 32% -- -1% -4% + -5% Old way L 354610/s 56% 34% 1% -- -3% + -4% Short U 364964/s 61% 38% 4% 3% -- + -1% Short L 367647/s 62% 39% 5% 4% 1% + -- Rate Match TR LC Match TR UC Only TR UC Only TR LC N +ew way U New way L Old way U Old way L Short lc L Short lc +U Short way U Short way L Match TR LC 185874/s -- -10% -12% -12% + -18% -19% -48% -48% -48% -49 +% -55% -55% Match TR UC 207469/s 12% -- -1% -2% + -8% -10% -41% -42% -42% -43 +% -49% -50% Only TR UC 210084/s 13% 1% -- -1% + -7% -8% -41% -41% -42% -42 +% -49% -49% Only TR LC 211864/s 14% 2% 1% -- + -6% -8% -40% -41% -41% -42 +% -48% -49% New way U 226244/s 22% 9% 8% 7% + -- -1% -36% -37% -37% -38 +% -45% -45% New way L 229358/s 23% 11% 9% 8% + 1% -- -35% -36% -36% -37 +% -44% -44% Old way U 354610/s 91% 71% 69% 67% + 57% 55% -- -1% -1% -2 +% -13% -14% Old way L 357143/s 92% 72% 70% 69% + 58% 56% 1% -- -1% -1 +% -13% -14% Short lc L 359712/s 94% 73% 71% 70% + 59% 57% 1% 1% -- -1 +% -12% -13% Short lc U 362319/s 95% 75% 72% 71% + 60% 58% 2% 1% 1% - +- -12% -12% Short way U 409836/s 120% 98% 95% 93% + 81% 79% 16% 15% 14% 13 +% -- -1% Short way L 413223/s 122% 99% 97% 95% + 83% 80% 17% 16% 15% 14 +% 1% --

- quite the opposite of what you report. From the first block of results, doing a lc on $_[0] and no assignments at all

sub short_lc { split( /\./, lc($_[0]),2 ) }

seems to be fastest, as expected. Lowercase vs. uppercase of its argument doesn't make any difference (it is too small to be reliable and could change with the order the anonsubs where introduced).

update: changed some wording

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Premature and micro optimization...
by chargrill (Parson) on Jun 11, 2007 at 12:15 UTC

    You're absolutely right. :)

    I trimmed down my code before posting - without running it - which would've immediately pointed out the problem.

    See my post further on down the thread.


    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2019-09-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (251 votes). Check out past polls.

    Notices?