in reply to Re: undef speedup ?!
in thread undef speedup ?!

Well, you got me that time. Of course I know what 'my' does. I didn't plan for this format to create a completely different lexical scope:

timethese 10000, { this => ' ...... ', that => ' ...... ' };

When benchmarking I'm accustomed to using the sub format, like this:

timethese 10000, { this => sub { ..... }, that => sub { ..... } }

...which follows a more natural lexical scoping, where the subs are still executed within the same scope as the lexical block in which they're called.

For the record, the following code now matches the OP's observed behavior:

use strict; use warnings; use Benchmark; my (%h_delete, %h_list, %h_undef); %h_delete = %h_list = %h_undef = (1 .. 50000); delete @h_delete{keys %h_delete}; %h_list = (); undef %h_undef; timethese 10000, { delete => sub{ 1 for keys %h_delete}, list => sub{ 1 for keys %h_list}, undef => sub{ 1 for keys %h_undef}, };


Dave

Replies are listed 'Best First'.
Re: undef speedup ?!
by Abigail-II (Bishop) on Feb 09, 2004 at 17:39 UTC
    I tend to not use the sub variant, but the string variant. Using a sub means entering and leaving a scope, and that takes time. If that time is significant compared to what you are benchmarking, your results are far less useful than they could be. Watch:
    #!/usr/bin/perl use strict; use warnings; use Benchmark qw /cmpthese/; our ($x, $y); cmpthese -1 => { sub => sub {$::x ++}, string => '$::y ++', } __END__ Rate sub string sub 9175039/s -- -29% string 12862205/s 40% --

    Abigail