in reply to pop() on a fast multi-core cpu

I always (well, sometimes) enjoy trying my hand at rewriting other people's crap code.

#!/usr/bin/perl use Time::HiRes qw(gettimeofday tv_interval); use List::Util 'shuffle'; # perlfaq4 use warnings; use strict; my @ups = ('0') x 10; # four suits are added to a deck and then shuffled my @deck = shuffle(('A' .. 'M') x 4); #print "@deck\n"; exit 0; print "\n"; my $t0 = gettimeofday; $ups[0] = shift @deck; eval { my $done = 0; while (!$done) { print "@ups\n"; $done = 1; scan: for (my $j=1;$j<10;$j++) { $ups[$j] eq '0' and $ups[$j] = (shift @deck or die); for (my $k=0;$k<$j;$k++) { if ( $ups[$j] eq $ups[$k] ) { $ups[$j] = (shift @deck or die); $ups[$k] = (shift @deck or die); $done = 0; last scan; } } } } }; print "\n@ups\tExit row\n"; my $usec = sprintf("%.2f", (gettimeofday - $t0) * 10**6); print "\n".(52-@deck)." passes in $usec microseconds. "; print !@deck ? "Succeeded in consuming the entire deck.\n\n" : "Failed to consume the entire deck.\n\n";

Of course, if you have input/output, such as those print statements, there's virtually no meaning to any timings you get.

One thing I don't like about the OP's solution (which I didn't change in mine) is that there is a lot of unnecessary checking on every pass. For example, if on pass N we cover spots 5 and 6, then there's no need to check on pass N+1 whether spots 3 and 4 are a pair.

Replies are listed 'Best First'.
Re^2: pop() on a fast multi-core cpu
by tybalt89 (Monsignor) on Oct 31, 2024 at 03:15 UTC

    TIMTOWTDI - or perhaps I misunderstand the problem...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11162510 use warnings; use Time::HiRes qw( time ); use List::AllUtils qw( shuffle ); my @stock = shuffle split //, 'A23456789TJQK' x 4; my $ninecardlist = join '', splice @stock, 0, 9; my $start = time; while( @stock > 1 ) { print "$ninecardlist\n"; my ($left, $right) = splice @stock, 0, 2; $ninecardlist =~ s/(.)(.*?)\1/ print "play $left & $right over $1's\n"; "$left$2$right"/e or last; } @stock > 1 or print "$ninecardlist\n"; print @stock < 2 ? "Succeeded in consuming the entire deck.\n" : "Failed to consume the entire deck.\n"; printf "%d passes in %d microseconds\n", 52 - @stock, (time - $start) +* 1e6;

    Outputs:

    T7AJJQ342 play A & 9 over J's T7AA9Q342 play 8 & T over A's T78T9Q342 play T & 3 over T's T7839Q342 play 8 & 5 over 3's T7889Q542 play 5 & 6 over 8's T7569Q542 play 6 & 7 over 5's T7669Q742 play 9 & 8 over 7's T9669Q842 play A & 6 over 9's TA666Q842 play 4 & K over 6's TA4K6Q842 play 3 & A over 4's TA3K6Q8A2 play Q & J over A's TQ3K6Q8J2 play 7 & 6 over Q's T73K668J2 play T & 4 over 6's T73KT48J2 play K & Q over T's K73KQ48J2 play 7 & 3 over K's 7733Q48J2 play 2 & 4 over 7's 2433Q48J2 play 8 & 9 over 2's 8433Q48J9 play J & 2 over 8's J433Q42J9 play 9 & K over J's 9433Q42K9 play 2 & K over 9's 2433Q42KK play Q & 5 over 2's Q433Q45KK Succeeded in consuming the entire deck. 51 passes in 125 microseconds

      Regexes! I love it!
      It just goes to show:
      TIAABWTDI (There is always a better way to do it) :-)

      Note the OP used a ten card list (or am I missing something?)
        "The code sequence causing the problem was : search a nine card list for a pair, "