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

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

by TJPride (Pilgrim)
on Nov 25, 2011 at 11:58 UTC ( #940034=note: print w/replies, xml ) Need Help??


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

I would think that substr would be fast enough to do what you need. The main bottleneck, as BrowserUK said, is probably not substr, but rather disk read (if you're reading one record at a time) or memory allocation. On the former, you could try using read() and a buffer variable. Be simple enough to read in some large multiple of 10 (9 + separator character) and then generate directly from that:

use strict; use warnings; my ($handle, $buffer, $size, @parsed, $i); open($handle, 'my-data.txt'); while ($size = read($handle, $buffer, 30)) { for ($i = 0; $i < $size; $i += 10) { push @parsed, [ substr($buffer, $i, 3), substr($buffer, $i+3, 3), substr($buffer, $i+6, 3) ]; } }

Replies are listed 'Best First'.
Re^2: Fast Way to Split String in to Chunk of Equal Length
by TJPride (Pilgrim) on Nov 25, 2011 at 16:48 UTC
    Note that I was testing with a read of 30 - should have modified it back to 10240 before posting.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-04-05 10:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (33 votes). Check out past polls.

    Notices?