Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The fastest way I've found of accessing the characters of a string is to use chop.

Even if you need to copy the string to avoid its destruction; and reverse it to get the characters in the right order, it still comes out substantially quicker than any other method I've tried.

If you can avoid both the copy and the reverse it gets much faster still, but that's not easy to benchmark due to the destructive process. But if you are reading the records one at a time from a file, you're probably going to overwrite the record at each iteration, so that often doesn't matter in a real application.

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $string = 'x'x5000; cmpthese -1, { substr => q[ for ( 0..length( $string)-1 ) { my $c = substr $string, $_, 1; } ], splitArray => q[ my @c=split'',$string; for( 0 .. $#c ){ my $c = $c[$_]; } ], splitFor => q[ for( split'', $string ){ my $c = $_; } ], unpack => q[ for( unpack 'C*', $string ) { my $c = chr; } ], reverseChop => q[ my $s = reverse $string; my $c; $c = $_ while chop $s; ], chop => q[ my $s = $string; my $c; $c = $_ while chop $s; ], ramfile => q[ open my $ram, '<', \$string; my $c; $c = $_ while $_ = getc( $ram ); ], }; __END__ C:\test>873068 Rate splitArray splitFor ramfile substr unpack reverseCh +op chop splitArray 169/s -- -48% -74% -81% -83% -8 +9% -89% splitFor 323/s 91% -- -51% -64% -67% -7 +9% -79% ramfile 654/s 288% 103% -- -27% -34% -5 +7% -58% substr 891/s 428% 176% 36% -- -9% -4 +2% -43% unpack 984/s 484% 205% 51% 10% -- -3 +6% -37% reverseChop 1534/s 809% 376% 135% 72% 56% +-- -1% chop 1555/s 822% 382% 138% 74% 58% +1% --

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Efficient walk/iterate along a string by BrowserUk
in thread Efficient walk/iterate along a string by gje21c

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (9)
    As of 2018-03-18 14:02 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (230 votes). Check out past polls.