Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Merge 2 strings like a zip

by BrowserUk (Patriarch)
on Jul 09, 2015 at 03:11 UTC ( [id://1133865]=note: print w/replies, xml ) Need Help??


in reply to Re: Merge 2 strings like a zip
in thread Merge 2 strings like a zip

Hm.

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use List::MoreUtils qw[ zip ]; sub zipA { my( $str1, $str2 ) = @_; $str1 =~ s/.\K/ substr $str2, 0, 1, ''/gesr; } sub zipB { no warnings qw/ uninitialized /; my( $a, $b ) = @_; my @a1 = split( '', $a ); my @a2 = split( '', $b ); return join'', zip @a1, @a2; } sub zipC($$){ my( $n, $a, $b ) = ( 1, @_ ); substr( $a, $n, 0, $_), $n += 2 for split '', $b; return $a; };; our $A = 'ABCDEFGHIJ'; our $B = 'abcde'; cmpthese -1, { A => q[ my $zipped = zipA( $A, $B ); ], B => q[ my $zipped = zipB( $A, $B ); ], C => q[ my $zipped = zipC( $A, $B ); ], }; __END__ C:\test>\perl5.18\perl\bin\perl.exe 1133857.pl Rate B A C B 43932/s -- -48% -72% A 84167/s 92% -- -47% C 159444/s 263% 89% --

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.
I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

Replies are listed 'Best First'.
Re^3: Merge 2 strings like a zip
by SimonPratt (Friar) on Jul 09, 2015 at 10:43 UTC
    "substr( $a, $n, 0, $_), $n += 2 for split '', $b;"

    Excellent use of substr

Re^3: Merge 2 strings like a zip
by 1nickt (Canon) on Jul 09, 2015 at 04:03 UTC

    The race is not always to the swift, my friend.

    Remember: Ne dederis in spiritu molere illegitimi!

      1nickt:

      If it's a race, I'd expect the swift to win... ;^)

      On a more serious note: Pulling in a module for such a simple task doesn't seem as good as simply making a simple subroutine and giving it a good name.

      Certainly, for more intricate operations, modules can be very useful. But if you need a particular small routine (like this one), and it servers a particular purpose correctly (like this one), and it's unlikely to need maintenance (like this one), then I think a well-named subroutine beats the module seven days a week. That's the same reason I don't use File::Slurp.

      For my projects, I frequently have several utility functions I use repeatedly. But rather than pull in several different modules each for a different little thing like this, I just keep my MCM::Utils module that contains all of the ones I frequently use. This way, I pull in one module, and have all of them. (Perhaps I ought to clean it up and publish a 'Robojunk' module to cpan module...)

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        If it's a race, I'd expect the swift to win... ;^)

        Not always. There are sprints, where that could be assumed. But there are also marathons, steeplechases, and orienteering, among others. So stamina, fortitude, planning ... lots of things, can come into play besides just swiftness. I think that's where the saying comes from. That and the tale of the tortoise and the hare ...

        I agree with your point about modules potentially causing bloat. But in the case of the average beginner, using a well-established module for a common task will usually not cause that problem, but _will_ allow him/her to get back to the true task at hand. If using the module becomes inefficient or the wrong choice for some other reason, then by all means roll your own.

        I have some collections of tools in modules, too. Always a balance between convenience of having them in one file vs. the bloat, again, of loading stuff you don't need. Lately I am loving Exporter::Tiny for that. No non-Core dependencies, ::Tiny-style minimal code to use ... very convenient.

        Every situation is different.

        Remember: Ne dederis in spiritu molere illegitimi!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found