Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi

I never liked the usage of the CODEHASHREF in Benchmark.

Often people start writing things like

sub name1 { ... } sub name2 { ... } cmpthese ( -5, { 'name1' => \&name1, 'name2' => \&name2, } )

which isn't very DRY and makes experimenting with optimizations really cumbersome! (I hate it, every new function name has to be repeated 3 times in different locations...)

see also Re: Best method to diff very large array efficiently for another examle.

My idea is to put all subs into a dedicated package (defaultname "CMP" or so) and to automatically filter necessary name and coderefs.

{ package CMP; sub name1 { ... } sub name2 { ... } } cmpthese (-5, pckg_subs("CMP") )

ATM I'm using a function pckg_subs() for this, not sure if it makes sense to extend the interface of cmpthese and timethese to directly accept a stash-ref like \%CMP::. ¹

Following a proof on concept, request for comments.

use strict; use warnings; use Benchmark qw/cmpthese/; use Data::Dump qw/pp/; { package CMP; my @arr_1 = map {rand 1e6} 8000; my @arr_2 = map {rand 1e6} 6000; sub hash_values_diff { my %diff3; @diff3{@arr_1} = @arr_1; delete @diff3{@arr_2}; values %diff3 ; } sub hash_key_diff { my %diff3; @diff3{@arr_1} = (); delete @diff3{@arr_2}; keys %diff3 ; } sub using_vec { my $vec = ''; vec( $vec, $_, 1 ) = 1 for @arr_2; grep !vec( $vec, $_, 1 ), @arr_1; } sub hash_grep { my %arr_2_hash; undef @arr_2_hash{@arr_2}; grep !exists $arr_2_hash{$_}, @arr_1; } } cmpthese(-5, pckg_subs() ); sub pckg_subs { my $pckg_name= shift // "CMP"; my $stash = do { no strict 'refs'; \ %{ "${pckg_name}::" }; }; # filter all subs from package my $codehashref; while (my ($name,$glob)= each %$stash) { if ( my $cref = *{$glob}{CODE} ) { print "$name:\t$glob\n"; $codehashref->{$name}=$cref; } } return $codehashref; }
OUTPUT
/usr/bin/perl -w /tmp/diff.pl hash_grep: *CMP::hash_grep hash_key_diff: *CMP::hash_key_diff hash_values_diff: *CMP::hash_values_diff using_vec: *CMP::using_vec Rate using_vec hash_values_diff hash_key_diff + hash_grep using_vec 22269/s -- -85% -90% + -91% hash_values_diff 148869/s 568% -- -31% + -40% hash_key_diff 215078/s 866% 44% -- + -13% hash_grep 247455/s 1011% 66% 15% + --

The idea code be extended with sub-attribute ':compare' or ':nocompare' to additionally mark functions which are supposed to be compared or not.

Cheers Rolf

( addicted to the Perl Programming Language)

update

¹) is it possible to tell if a hashref belongs to a stash?

... well at least I could parse %main or pass the packagename directly =)


In reply to RFC extending Benchmark.pm to facilitate CODEHASHREF by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-19 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found