Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by perlmonkey (Hermit)
on May 02, 2001 at 11:48 UTC ( #77258=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I pull n characters off the front of a string? - Answer contributed by perlmonkey

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
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2016-08-31 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (437 votes). Check out past polls.