Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: list of four digit lock combinations without repeated digits

by BrowserUk (Patriarch)
on Jun 21, 2018 at 04:34 UTC ( [id://1217092]=note: print w/replies, xml ) Need Help??


in reply to Re^2: list of four digit lock combinations without repeated digits
in thread list of four digit lock combinations without repeated digits

Am I doing something stupid with the module, or perl?
  1. This: for(@$c){ $x .= $_ } is a really slow way to build a string from an array.
  2. Package variables are slower than lexicals.
  3. Postfix loops generally run more quickly than those with scoped bodies.

Try this and see how it compares on your system:

perl -MAlgorithm::Combinatorics=:all -wle'my $i=variations_with_repeti +tion(["a".."z"],$ARGV[0]); my @x; push @x, qq[@$_] while $_=$i->next; + print scalar @x' 4

In general, those algorithms that require more selection than variations_with_repetition() -- almost any of the other algorithms -- is where A;:C shines over a pure perl implementation.


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

Replies are listed 'Best First'.
Re^4: list of four digit lock combinations without repeated digits
by usemodperl (Beadle) on Jun 21, 2018 at 08:18 UTC
    Very interesting info, thank you. I often wonder about such efficiency when writing perl but it doesn't usually matter too much. I do write postfix loops because they're pretty and one-line friendly (I ❤ print$_ for@_) but not when they're nested because I tend to forget about them. If they're really much faster I could use comments to remind myself. Anyway your version is slightly faster (~0.750) but the data has whitespace and when using map to remove that it gets even slower than mine. What's going on?

    time perl -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, map {s/ //gr} qq[@$_] while $_=$i->next; print scalar @x' 4
    real 0m1.050s

    Profile:
    perl -d:NYTProf -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, map {s/ //gr} qq[@$_] while $_=$i->next; print scalar @x' 4; nytprofhtml --open

    STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐫
      when using map to remove that it gets even slower than mine. What's going on?

      You are starting the regex engine 210 times.

      If you don't want the spaces, don't put them in to start with. Rather than "@$_" use join'',@$_ or pack 'A*', @$_, or set $"=undef.


      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
        If you don't want the spaces, don't put them in to start with. Rather than "@$_" use join'',@$_ or pack 'A*', @$_, or set $"=undef.

        Of course I picked the slowest way(s) not to do it. I've got a bad habit of overthinking problems and overengineering solutions and shoving a regex into map like that when I know all about join is typical! Your keisaku is appreciated, I can see:

        map + regex: ~0m0.950s
        time perl -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, map {s/ //} qq[@$_] while $_=$i->next; print scalar @x' 4
        map: ~0m0.830s
        time perl -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, map {$_} qq[@$_] while $_=$i->next; print scalar @x' 4
        pack: ~0m0.820s
        time perl -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, pack "A*", qq[@$_] while $_=$i->next; print scalar @x' 4
        join: ~0m0.720s
        time perl -MAlgorithm::Combinatorics=:all -wle' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, join "", @$_ while $_=$i->next; print scalar @x' 4
        $"=undef: ~0m0.720s
        time perl -MAlgorithm::Combinatorics=:all -le' my $i=variations_with_repetition(["a".."z"],$ARGV[0]); my @x; qq[$"=undef]; push @x, qq[@$_] while $_=$i->next; print scalar @x' 4
        STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐫

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found