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 | |
by jdporter (Paladin) on Oct 31, 2024 at 12:51 UTC | |
by ibm1620 (Hermit) on Oct 31, 2024 at 17:26 UTC | |
by tybalt89 (Monsignor) on Oct 31, 2024 at 18:34 UTC |