in reply to Re: Re: Re: Matching First Character of Strings Efficiently
in thread Matching First Character of Strings Efficiently
You use substr to get 1st char even though ord returns numeric value of 1st charI don't want to seem obstinate (I'm here for learning), but I made some tests:
You use split to get a list of individual characters and throw them away using a slice
#!/usr/bin/perl -w use strict; use Benchmark qw(:all); my $str_a = "dlajsdlkajslkdjasldjasljdaskjd"; cmpthese( -5, { 'Test1' => sub { ord( $str_a ) }, 'Test2' => sub { ord( ( split ( //, $str_a ) )[0] ) }, 'Test3' => sub { ord( substr $str_a, 0, 1 ) }, } ); __DATA__ Results: Rate Test2 Test3 Test1 Test2 6321/s -- -99% -100% Test3 516114/s 8065% -- -72% Test1 1827646/s 28815% 254% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Re: Matching First Character of Strings Efficiently
by Limbic~Region (Chancellor) on Mar 16, 2004 at 17:18 UTC | |
by kral (Monk) on Mar 16, 2004 at 20:13 UTC |