Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: CarTalk Puzzler

by Tanktalus (Canon)
on Nov 17, 2005 at 00:48 UTC ( [id://509252]=note: print w/replies, xml ) Need Help??


in reply to Re: CarTalk Puzzler
in thread CarTalk Puzzler

I'm just curious ... why are you using a hash? Hashes are great for string-based indexes or for sparse arrays. But here we have a contiguous array from 1 to 20,000. An array would not only be smaller, but faster. Lots faster. Even with your hash, we could get a bit more out of looping less. In your print, I'm not sure why you grep from the map. You could easily combine them as:

print join(', ', map { $lights{ $_ } ? $_ : () } ( 1..20_000) )."\n";
Even that is sub-optimal. After all, you want the original number - so you could just grep it out. I've put it as duckyd2 in my benchmark. And, for good measure, I've also added tanktalus as just the rewrite to use arrays.
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $duckyd_answer; sub duckyd { my %lights = map { $_ => 1 } ( 1..20_000 ); foreach my $flipper ( 2..20_000) { for( my $i = $flipper; $i <= 20_000; $i += $flipper ){ $lights{ $i } = !$lights{ $i }; } } $duckyd_answer = join(', ', grep { defined $_ } map { $lights{ $_ } ? $_ : undef } ( 1..20_000) ); } my $duckyd2_answer; sub duckyd2 { my %lights = map { $_ => 1 } ( 1..20_000 ); foreach my $flipper ( 2..20_000) { for( my $i = $flipper; $i <= 20_000; $i += $flipper ){ $lights{ $i } = !$lights{ $i }; } } $duckyd2_answer = join(', ', grep { $lights{ $_ } } ( 1..20_000) ); } my $tanktalus_answer; sub tanktalus { my @lights = (1) x 20_000; foreach my $flipper ( 2..20_000) { for( my $i = $flipper; $i <= 20_000; $i += $flipper ){ $lights[$i-1] = !$lights[$i-1]; } } $tanktalus_answer = join(', ', grep { $lights[$_-1] } 1..20_000); } cmpthese(-1, { duckyd => \&duckyd, duckyd2 => \&duckyd2, tanktalus => \&tanktalus, }, ); print "duckyd answer: $duckyd_answer\n"; print "duckyd2 answer: $duckyd2_answer\n"; print "tankalus answer: $tanktalus_answer\n";
And the results on this machine:
Rate duckyd duckyd2 tanktalus duckyd 47.3/s -- -1% -44% duckyd2 47.7/s 1% -- -44% tanktalus 84.6/s 79% 77% --
(I've removed the answers as they're all the same.) The 1% speed benefit of duckyd2 over duckyd is purely the removal of the map in the output string - mostly ignorable, I grant, as it's still O(n) vs the O(n^2) algorithm right before it. As you can see, arrays are significantly faster than hashes for this. Still O(n^2), but the constant is reduced ;-)

Replies are listed 'Best First'.
Re^3: CarTalk Puzzler
by BrowserUk (Patriarch) on Nov 17, 2005 at 03:51 UTC

    You can save more time and space by using a string of '1's & '0's, and a bit more still using a bitstring.

    sub buk1 { my $lights = '1' x ( 20_000 ); for my $gap ( 2 .. 20_000 ) { for( my $o = $gap; $o <= 20_000; $o += $gap ) { substr($lights, $o, 1) = substr($lights, $o, 1) eq '0' ? ' +1' : '0'; } } $answers{ buk1 } = join ', ', grep{ substr( $lights, $_, 1 ) } 1 . +. 20_000; } sub buk2 { my $lights = "\xFF" x ( 20_001 / 8 ); for my $gap ( 2 .. 20_000 ) { for( my $o = $gap; $o < 20_000; $o += $gap ) { vec( $lights, $o, 1 ) = ~vec( $lights, $o, 1 ); } } $answers{ buk2 } = join ', ', grep{ vec( $lights, $_, 1 ) } 1 .. 2 +0_000; } P:\test>junk Rate duckyd2 duckyd tanktalus buk buk2 duckyd2 1.96/s -- -1% -53% -66% -69% duckyd 1.99/s 1% -- -53% -66% -68% tanktalus 4.20/s 114% 111% -- -28% -33% buk 5.82/s 197% 193% 39% -- -7% buk2 6.27/s 220% 215% 49% 8% -- Comparing buk1 and buk2 Comparing buk2 and duckyd Comparing duckyd and duckyd2 Comparing duckyd2 and tanktalus

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: CarTalk Puzzler
by duckyd (Hermit) on Nov 17, 2005 at 19:56 UTC
    The only reason that I used a hash was that it was more fun to print the results with a hash than an array. My solution was not meant to be the most efficient, as it obviously wouldn't scale well at all.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found