Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: You don't always have to use regexes

by holli (Abbot)
on Feb 23, 2005 at 16:39 UTC ( [id://433753]=note: print w/replies, xml ) Need Help??


in reply to Re: You don't always have to use regexes
in thread You don't always have to use regexes

I benchmarked this and it yields an interesting result. index() is (a bit) faster than a regex. If itīs used in combination with lc(), as in your example, the regex with the i-modifier is faster.
use strict; use warnings; use Benchmark; my $value = "somewhere here true is there!"; timethese ( 9000000, { 'index' => sub { index( $value, "true" ) }, 'regex' => sub { $value =~ /true/ }, } ); timethese ( 9000000, { 'index' => sub { index( lc $value, "true" ) }, 'regex' => sub { $value =~ /true/i }, } ); Benchmark: timing 9000000 iterations of index, regex... index: 2 wallclock secs ( 2.02 usr + 0.00 sys = 2.02 CPU) @ 44 +46640.32/s (n=9000000) regex: 4 wallclock secs ( 2.40 usr + -0.01 sys = 2.39 CPU) @ 37 +60969.49/s (n=9000000) Benchmark: timing 9000000 iterations of index, regex... index: 4 wallclock secs ( 4.55 usr + 0.00 sys = 4.55 CPU) @ 19 +79762.43/s (n=9000000) regex: 3 wallclock secs ( 3.68 usr + 0.00 sys = 3.68 CPU) @ 24 +48313.38/s (n=9000000)


Update:
Ack. I really need to learn to type faster.


holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-25 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found