Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

( #77258=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How do I pull n characters off the front of a string? 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)

Comment on Answer: How do I pull n characters off the front of a string?
Download Code
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 2014-12-20 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls