Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by BrowserUk (Pope)
on Jun 21, 2018 at 10:32 UTC ( #1217109=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^6: list of four digit lock combinations without repeated digits
by usemodperl (Beadle) on Jun 22, 2018 at 02:35 UTC
    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 🐫
       push @x, pack "A*", qq[@$_] while $_=$i->next;

      That would probably match if not beat join, if you weren't stringifying the array twice.

      Ie. Use  push @x, pack "(A*)*", @$_ while $_=$i->next;

      Of course, if you want to put all the sequences as strings into an array, this is easier and probably quicker;

      my @x = map join( '', @$_ ), variations_with_repetition( ["a".."z"], 4 + );; print scalar @x;; 456976

      And if all you want to do is print the count, then:

      [ 7:10:21.87] C:\test>perl -MAlgorithm::Combinatorics=:all -le"print s +calar( () = variations_with_repetition( ["a".."z"], $ARGV[0] ) )" 4 456976 [ 7:10:27.12] C:\test>

      BTW, that is one seriously quick machine you're running. What is it? (6x faster than my (admittedly ancient) box.


      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
        That would probably match if not beat join, if you weren't stringifying the array twice.

        Actually you did that previously and I carelessly cargo-culted it into my code.
        However upon testing it's actually very slightly faster! I don't know why:
        perl -MBenchmark=:all -MAlgorithm::Combinatorics=:all -wle' for ("1".."4") { my $t0 = Benchmark->new; my $i = variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, pack "(A*)*", @$_ while $_=$i->next; my $t1 = Benchmark->new; my $ts = timestr(timediff($t1,$t0)); print $ts }' 4
        1 wallclock secs ( 0.77 usr +  0.01 sys =  0.78 CPU)
        1 wallclock secs ( 0.77 usr +  0.01 sys =  0.78 CPU)
        1 wallclock secs ( 0.75 usr +  0.00 sys =  0.75 CPU)
        1 wallclock secs ( 0.75 usr +  0.01 sys =  0.76 CPU)
        
        
        
        perl -MBenchmark=:all -MAlgorithm::Combinatorics=:all -wle' for ("1".."4") { my $t0 = Benchmark->new; my $i = variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, pack "A*", qq[@$_] while $_=$i->next; my $t1 = Benchmark->new; my $ts = timestr(timediff($t1,$t0)); print $ts }' 4
        0 wallclock secs ( 0.74 usr +  0.01 sys =  0.75 CPU)
        1 wallclock secs ( 0.72 usr +  0.01 sys =  0.73 CPU)
        1 wallclock secs ( 0.73 usr +  0.00 sys =  0.73 CPU)
        1 wallclock secs ( 0.74 usr +  0.01 sys =  0.75 CPU)
        
        
        
        perl -MBenchmark=:all -MAlgorithm::Combinatorics=:all -wle' for ("1".."4") { my $t0 = Benchmark->new; my $i = variations_with_repetition(["a".."z"],$ARGV[0]); my @x; push @x, join "", @$_ while $_=$i->next; my $t1 = Benchmark->new; my $ts = timestr(timediff($t1,$t0)); print $ts }' 4
        1 wallclock secs ( 0.65 usr +  0.01 sys =  0.66 CPU)
        1 wallclock secs ( 0.64 usr +  0.00 sys =  0.64 CPU)
        0 wallclock secs ( 0.63 usr +  0.00 sys =  0.63 CPU)
        1 wallclock secs ( 0.64 usr +  0.01 sys =  0.65 CPU)
        
        
        
        BTW, that is one seriously quick machine you're running. What is it?
        
        MacBook Pro i7
        3.1GHz (4.1GHz Turbo Boost) 16GB RAM 1TB SSD
        Darwin 10.13.4
        
        
        STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐪

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2019-05-22 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (138 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!