in reply to Re: Matching First Character of Strings Efficiently
in thread Matching First Character of Strings Efficiently
Just looking at the faster versions and removing everything that is not the function (ie one true, one false test makes a set)...
#!/usr/bin/perl use Inline C =>; use Benchmark 'cmpthese'; # Rate tchn tchn2 kval LR tye2 tye1 # tchn 556060/s -- -9% -19% -20% -50% -64% # tchn2 611783/s 10% -- -11% -12% -45% -60% # kval 690724/s 24% 13% -- -0% -38% -55% # LR 691904/s 24% 13% 0% -- -38% -55% # tye2 1114446/s 100% 82% 61% 61% -- -28% # tye1 1542899/s 177% 152% 123% 123% 38% -- my $str_a = 'a'x255; my $str_b = 'b'x255; my $str_c = $str_a; my ( $fc ); cmpthese -5, { 'tchn' => sub { $a = same_scan( $str_a, $str_b ); $a = same_scan( $str_a, $str_c ); }, 'tchn2' => sub { $a = same_scan2( $str_a, $str_b ); $a = same_scan2( $str_a, $str_c ); }, 'LR' => sub { $a = index($str_a, substr($str_b, 0, 1)); $a = index($str_a, substr($str_b, 0, 1)); }, 'tye1' => sub { $a = ord $str_a != ord $str_b; $a = ord $str_a != ord $str_c; }, 'tye2' => sub { $fc = ord $str_a; $a = $fc != ord( $str_b ); $fc = ord $str_a; $a = $fc != ord( $str_b ); }, 'kval' => sub { $fc = substr $str_a, 0, 1; $a = $fc ne substr $str_b, 0, 1; $fc = substr $str_a, 0, 1; $a = $fc ne substr $str_c, 0, 1; }, }; __END__ __C__ int same_scan(char* str1, char* str2) { return str1[0] == str2[0] ? 1 : 0; } int same_scan2(SV* str1, SV* str2) { return SvPV(str1, PL_na)[0] == SvPV(str2,PL_na)[0] ? 1 : 0; }
cheers
tachyon
|
|---|