Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Fast Way to Split String in to Chunk of Equal Length

by BrowserUk (Pope)
on Nov 25, 2011 at 10:31 UTC ( #940021=note: print w/replies, xml ) Need Help??


in reply to Fast Way to Split String in to Chunk of Equal Length

unpack is the way to go, but one tip is that unpack 'a3' x $n, $str runs a few percent quicker than unpack '(a3)*', $str if you know in advance how many fields there are.

Also, if your problem allows you to unpack the strings just in time at the point where you need them, rather than en masse, avoiding the allocation, creation and destruction of 10 million concurrent small arrays -- even if you have ample memory for them -- can save a significant amounts of time.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

Replies are listed 'Best First'.
Re^2: Fast Way to Split String in to Chunk of Equal Length
by ikegami (Pope) on Nov 25, 2011 at 17:09 UTC

    unpack is the way to go, but one tip is that unpack 'a3' x $n, $str runs a few percent quicker than unpack '(a3)*', $str if you know in advance how many fields there are.

    How can you not know in advance how many fields there are (int(length($seq)/3))?

    avoiding the allocation, creation and destruction of 10 million concurrent small arrays -- even if you have ample memory for them -- can save a significant amounts of time

    Any implementation in mind? Something like the following?

    while ($seq =~ /(.{3})/sg) { # Substring in $1 ... }

      Assuming your question is in regard to:

      Also, if your problem allows you to unpack the strings just in time at the point where you need them, rather than en masse, avoiding the allocation, creation and destruction of 10 million concurrent small arrays -- even if you have ample memory for them -- can save a significant amounts of time.

      I meant -- unpack the strings just in time at the point where you need them -- something like:

      for my $seq ( @seqs ) { my @bits = unpack 'a3a3a3', $seq; ## $bits[ ... ]; }

      rather than -- en masse --:

      @seqs = map[ unpack '(a3)*', $_ ], @seqs; for my $ref ( @seqs ) { ## $ref->[ ... ] }

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

      At it again. Unannounced updates. The above post originally consisted entirely of just:

      Any implementation in mind? Something like the following?
      while ($seq =~ /(.{3})/sg) { # Substring in $1 ... }

      To answer the dumb question that wasn't there originally,

      How can you not know in advance how many fields there are (int(length($seq)/3))?

      If you have to calculate the number, you throw away the few percent gained.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2020-04-08 15:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (44 votes). Check out past polls.

    Notices?