Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [ambrus]: ah, it's one of thos
    [Eily]: what, there's a difference between proof of concept and production?
    [LanX]: 20 years ago traders were complaining about the latency of the trading system...
    [ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
    [ambrus]: ething that's both cleanly organized and mostly automated.
    LanX in train, bad connection
    [Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
    [LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
    [ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (16)
    As of 2017-03-29 11:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should Pluto Get Its Planethood Back?



      Results (350 votes). Check out past polls.