rsFalse has asked for the wisdom of the Perl Monks concerning the following question:
First one works slower. On my PC to transpose 2000 lines of 2000 chars length it worked for: 1 sec with split, 0.6 sec with chop. Both ways are non-destructive (after using map in second).for( @original ){ my $i = 0; $transposed[ $i ++ ] .= $_ for split //; } for( map $_ = reverse, @original ){ my $i = 0; $transposed[ $i ++ ] .= chop while $_; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to transpose lines faster?
by pryrt (Abbot) on Feb 01, 2018 at 22:03 UTC | |
I would have used with_substr() (in the spoiler), but your chop-based is faster during my test. However, if the character '0' (ASCII 48) is in your possible alphabet, chop() won't give the right answer benchmark:
I am no good with pack/unpack, otherwise, I would have tested LanX's suggestion, too. From what I remember in past benchmarks, that pair usually beats substr. update: chop will fail because you're testing the truthiness of the character it returns. '0', like 0, is false. update 2: hmm, no, you should be testing the truthiness of the whole string. When I had '0' in my alphabet, it dropped one or more '0's from the last row of @$wch... Oh, right, if the last character in one of the strings in @original ends in one or more zeroes, then it will evaluate to false, rather than your expected true. So if '0' might be in your alphabet, you should probably test the if length($_) instead. update 3: I meant while length $_... | [reply] [d/l] [select] |
by pryrt (Abbot) on Feb 01, 2018 at 22:23 UTC | |
yeah, the while length $_ will do it; see modified code
| [reply] [d/l] [select] |
by rsFalse (Chaplain) on Feb 02, 2018 at 12:00 UTC | |
| [reply] |
|
Re: How to transpose lines faster?
by AnomalousMonk (Archbishop) on Feb 01, 2018 at 22:50 UTC | |
Both ways are non-destructive (after using map in second). FWIW: I don't understand what "after using map in second" means, but the second OPed method is destructive (i.e., changes @orig array): But changing the map expression to for (map scalar(reverse), @orig) { ... } fixes this. Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
|
Re: How to transpose lines faster?
by LanX (Saint) on Feb 01, 2018 at 20:50 UTC | |
Probably by using unpack and pack with a generated dynamic template.
Cheers Rolf
| [reply] |
|
Re: How to transpose lines faster?
by BrowserUk (Patriarch) on Feb 02, 2018 at 00:38 UTC | |
How does this score on your system?
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". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.
Suck that fhit
| [reply] [d/l] |
by rsFalse (Chaplain) on Feb 03, 2018 at 00:25 UTC | |
$transposed[ $i ++ ] .= chop while $_ (incorrect): 0.6s $transposed[ $i ++ ] .= chop while length (correct) (by pryrt): 0.75s $transposed[ $i ++ ] .= chop while /./ (assuming that dot match any input char): 1.35s $transposed[ $i ++ ] .= chop while !/^$/: 1.35s next are without reverse: $transposed[ $i ++ ] .= $& while /./g: 1.85s $transposed[ $i ++ ] .= substr $_, 0, 1, '' while length: 1.0s $transposed[ $i ++ ] .= $_ for split //: 1.0s push @out, join '', map substr( $_,$i,1 ), @in (by BrowserUK): 1.1s (pryrt's substr+substr variant ommited) | [reply] [d/l] [select] |
by pryrt (Abbot) on Feb 03, 2018 at 02:03 UTC | |
I'm actually curious how my multi-substr variant would perform in your bench... because in my tests (on two different machines), and much to my surprise, mine significantly outperforms the join-map-substr.
And yes, the anomalous_map, anomalous_two and with_chop_fixed seemed to go back and forth a lot, even on the first system. It's not had a lot of runs, so the timing's not very accurate. Trying with 200x200 on the second machine, to give it more runs per function:
<Reveal this spoiler or all in this thread> | [reply] [d/l] [select] |
|
Re: How to transpose lines faster?
by AnomalousMonk (Archbishop) on Feb 02, 2018 at 18:20 UTC | |
Here's my nomination. I haven't done any Benchmark-ing, but the minimal computation done in the inmost for-loop gives me hope that it will have a chance. Read more... (5 kB)
Update: It just occurred to me that pre-extending the @transposed array might squeeze out a few more microseconds of performance. In the AnomalousMonk_1() function, change the Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |