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

add my (%h_delete, %h_list, %h_undef); at the appropriate place near the top of the script,
That would not be a smart thing to do. You do know what the effect of my is, don't you? It makes its argument(s) lexical variables. Meaning that those variables are visible only in the current block, and not from other blocks or files. Which means, not from the Benchmark.pm file either. The Benchmarking code will access the package variables %h_delete, %h_list, %h_undef - just like the program would if you leave out the my.

The important lesson to be learned is that one should never blindly apply my to variables. Always know what you are doing, and always consider the consequences.

Abigail

Replies are listed 'Best First'.
Re: Re: undef speedup ?!
by davido (Cardinal) on Feb 09, 2004 at 16:57 UTC
    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

      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