On Feb 11, 2004, at 9:23 AM, Jan Eden wrote:


James Edward Gray II wrote:


Now it might be hard to determine which of these two is faster.

And the results, from my G5:

Benchmark: timing 3000000 iterations of all_ones, foreach, hash_slice,
map_hash, map_ones, range...
  all_ones:  1 wallclock secs ( 1.76 usr +  0.00 sys =  1.76 CPU) @
1704545.45/s (n=3000000)
   foreach:  3 wallclock secs ( 2.39 usr +  0.00 sys =  2.39 CPU) @
1255230.13/s (n=3000000)
hash_slice:  1 wallclock secs ( 0.94 usr +  0.00 sys =  0.94 CPU) @
3191489.36/s (n=3000000)
  map_hash:  2 wallclock secs ( 1.43 usr +  0.00 sys =  1.43 CPU) @
2097902.10/s (n=3000000)
  map_ones:  4 wallclock secs ( 3.91 usr +  0.00 sys =  3.91 CPU) @
767263.43/s (n=3000000)
     range:  1 wallclock secs ( 2.20 usr +  0.00 sys =  2.20 CPU) @
1363636.36/s (n=3000000)

Looks like Rob is still the man to beat. :D

Dang. Ok, Rob's original idea is the fastest solution, but he has to use exists later.

I think you're in for a surprise:


#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

my %is_blue = ( teal => 1 );

timethese( 10000000, {
        true_value      => '$is_blue{teal}',
        exists          => 'exists $is_blue{teal}'
} );

__END__

And again, my results:

Benchmark: timing 10000000 iterations of exists, true_value...
exists: 2 wallclock secs ( 1.65 usr + 0.00 sys = 1.65 CPU) @ 6060606.06/s (n=10000000)
true_value: 2 wallclock secs ( 2.25 usr + 0.00 sys = 2.25 CPU) @ 4444444.44/s (n=10000000)


Of the solutions not using exists, I come in third (range). That's not bad for an amateur or is it? ;)

Not bad at all. Your solution was clever, I think, and may even have practical uses, since it retains an array order in the hash values.


The real important thing to remember here though is just how insignificant this all is! Take a look up and see how many iterations I had to run these benchmarks through, just to get significant enough results to measure. Is your if/else chain going to have 10,000,000 branches? If so, Rob knows how to save you less than one second. ;)

Write good code. Make that your number one goal. Don't obsessively save keystrokes, don't fine tune for breakneck speeds. Just write good code. From there, the rest is easy. Trust me. ;)

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to