in reply to tr faster than eq?
And now the results:use strict; use warnings; use Benchmark qw(cmpthese); my $string = '0' x 13; cmpthese( 0, { 'eq_loop_string' => q{ for ( 0 .. 12 ) { next if ( $string eq '0000000000000' ); $_ = ( $string =~ tr/1/1/ ); } }, 'eq_loop_sub' => sub{ for ( 0 .. 12 ) { next if ( $string eq '0000000000000' ); $_ = ( $string =~ tr/1/1/ ); } }, 'tr_loop_string' => q{ for ( 0 .. 12 ) { next unless ( $_ = ( $string =~ tr/1/1/ ) ); } }, 'tr_loop_sub' => sub{ for ( 0 .. 12 ) { next unless ( $_ = ( $string =~ tr/1/1/ ) ); } }, 'eq' => q($string eq '0000000000000';), 'tr' => q($string =~ tr/1/1/;), } );
Rate eq_loop_string tr_loop_sub tr_loop_string eq_loop_sub tr eq
eq_loop_string 124852/s -- -8% -15% -38% -97% -98%
tr_loop_sub 136139/s 9% -- -7% -32% -97% -98%
tr_loop_string 147082/s 18% 8% -- -27% -97% -98%
eq_loop_sub 200481/s 61% 47% 36% -- -95% -97%
tr 4356550/s 3389% 3100% 2862% 2073% -- -42%
eq 7530073/s 5931% 5431% 5020% 3656% 73% --
Is it possible that perl was having to eval the strings each time the sub was called, thus skewing the results?
thor
Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: tr faster than eq?
by ikegami (Patriarch) on Mar 10, 2005 at 18:29 UTC | |
by thor (Priest) on Mar 10, 2005 at 19:22 UTC | |
by ikegami (Patriarch) on Mar 10, 2005 at 19:41 UTC | |
|
Re^2: tr faster than eq?
by Anonymous Monk on Mar 11, 2005 at 09:23 UTC |