pedrete has asked for the wisdom of the Perl Monks concerning the following question:
HI monks.. I have searched for posts but not found anything... Is there a faster way of splitting a string into an array based on carriage returns? Currrently i am using
my @array = split (/\n/,$HugeString);Thanks....
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Fastest split possible
by davido (Cardinal) on Nov 06, 2019 at 20:07 UTC | |
Where did the string come from in the first place? If the carriage returns are of a predictable format (always \n, or always \r\n) you can use index to find the next one. This will add code complexity, and pull more work into your code, and less out of Perl's underlying C implementation. But it will avoid invoking the regex engine. Which one wins would be up to how you code it, and also really up to that tradeoff of doing more work in Perl versus doing more work in perl. On the other hand, if you're reading this big string in as a file, your record separator $/ and <$fh> should handle it. That leads to one additional approach:
Again not sure that will be faster, but an in-memory filehandle to a scalar is a nice little trick. It definitely puts more of the work into perl and less into Perl. Update: I've since done my own benchmarks. The ones shown elsewhere in this thread are indicative of the results I was getting. The gist is that it's pretty hard to beat split. Keep in mind though, that if you're splitting a huge string into an array it's possible you're blowing up memory, and this would happen whether you use split, or some other solution. Back to my solution of opening an in-memory handle; it seems to be slower than split considerably. It my be neat using the technique to get an easy iterator for a string, but it's not going to be faster than split. Dave | [reply] [d/l] [select] |
by AnomalousMonk (Archbishop) on Nov 06, 2019 at 20:37 UTC | |
That's simpler and probably faster than what I had in mind here. Combined with an idea from Discipulus:
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
Re: Fastest split possible
by afoken (Chancellor) on Nov 06, 2019 at 20:52 UTC | |
Is splitting at newlines really your bottleneck? Or are you microoptimizing irrelevant parts of your code? Try Devel::NYTProf, generate a report (run nytprofhtml) and look for code that is highlighted in red, because it is slow or runs very often. That's where you want to optimize for speed. (See also https://www.slideshare.net/Tim.Bunce/nyt-prof-201406key) Everything else should be optimized for maintainance, i.e. keep your code clear, readable, and documented. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] |
Re: Fastest split possible
by marioroy (Prior) on Nov 07, 2019 at 00:29 UTC | |
Update: Added regex example. Hello all :) I tried trimming the left side of the string. Plus incorporated the memfh example by davido. I'm curious too. Split improves ~ 2x faster for this demonstration with Perl 5.22.x and later releases. That is the case on macOS.
Output - Perl 5.28.2
Another machine - Perl 5.26.1
Same machine - Perl 5.18.2
Regards, Mario | [reply] [d/l] [select] |
by marioroy (Prior) on Nov 08, 2019 at 03:22 UTC | |
Update 1: Added walk2 by pushing to stack. Ah, I tried Inline::C. Walk1 runs faster than split in older Perl <= 5.20.x.
Output from Perl 5.20.3 and Perl 5.26.1
Regards, Mario | [reply] [d/l] [select] |
Re: Fastest split possible
by haukex (Archbishop) on Nov 06, 2019 at 20:18 UTC | |
There was a bunch of benchmarking done in this thread I started (but you should probably do your own): Is foreach split Optimized? (Update: No.). opening a scalar, as davido suggested, is a good thing to try. | [reply] |
Re: Fastest split possible
by Discipulus (Canon) on Nov 06, 2019 at 19:49 UTC | |
to see it you can use Benchmark to test different approach, like capturing using regexes with g modifier. But probably, if you are experienceing slowness with split which is fast, you simply have to avoid to accumulate results into @array Infact if $HugeString is huge, would be probably better to process each element directly like in: process($_) for split /\n/,$HugeString; L*
There are no rules, there are no thumbs.. Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS. | [reply] [d/l] [select] |
Re: Fastest split possible (updated x2)
by AnomalousMonk (Archbishop) on Nov 06, 2019 at 20:09 UTC | |
For creating an array of substrings, split is probably fastest.
However, you usually want all those substrings so you can do something else with them. If you can process the string in place, it may be faster to do something like:
Update 1: The original code in my reply doesn't do what I imagined (update: the substr length operand is incorrect). Here's code that works as I intended and has a debug print to illustrate what's happening:
Update 2: This trick (update: i.e., substr returns an lvalue) also works if you want to pass a substring alias to a processing function, but the function has to operate directly on the aliased subroutine argument element in order to preserve the aliasing "chain". Also with an illustrative print point:
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
Re: Fastest split possible
by harangzsolt33 (Deacon) on Nov 07, 2019 at 03:41 UTC | |
Perl is beautiful. | [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |