in reply to Matching First Character of Strings Efficiently
Well the inline C solution is pretty short, dunno about faster or particularly creative though....
use Inline 'C'; my $str1 = 'foo'; for my $str2( qw( foo bar baz foxtrot perlmonks f ) ) { print "'$str1' cmp '$str2' result: ", same_scan($str1, $str2) ? "same\n" : "different\n"; } __END__ __C__ int same_scan(char* str1, char* str2) { return str1[0] == str2[0] ? 1 : 0; } /* 'foo' cmp 'foo' result: same 'foo' cmp 'bar' result: different 'foo' cmp 'baz' result: different 'foo' cmp 'foxtrot' result: same 'foo' cmp 'perlmonks' result: different 'foo' cmp 'f' result: same */
cheers
tachyon
|
|---|