Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How do I pull n characters off the front of a string?

by perlmonkey (Hermit)
on May 02, 2001 at 11:48 UTC ( [id://77258]=note: print w/replies, xml ) Need Help??


in reply to How do I pull n characters off the front of a string?

Use substr when possible, it can be many times faster then a simple regex:
my $a = "abcdefghijklmnopqrstuvwxyz"; use Benchmark; timethese(1000000, { 's1' => sub { s1($a, 5) }, 's2' => sub { s2($a, 5) }, }); sub s1 { my $a = $_[0]; $a =~ s/^.{$_[1]}//; return $a; } sub s2 { substr($_[0], $_[1]); } Results: s1: 8 wallclock secs ( 7.95 usr + 0.09 sys = 8.04 CPU) s2: 2 wallclock secs ( 2.54 usr + 0.01 sys = 2.55 CPU)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found