To get a lighter syntax you can already do something like :
my @arr_1 = map {rand 1e6} 8000;
my @arr_2 = map {rand 1e6} 6000;
my $cmp = {
hash_values_diff => sub {
my %diff3;
@diff3{@arr_1} = @arr_1;
delete @diff3{@arr_2};
values %diff3 ;
},
using_vec => sub {
my $vec = '';
vec( $vec, $_, 1 ) = 1 for @arr_2;
grep !vec( $vec, $_, 1 ), @arr_1;
},
hash_grep => sub {
my %arr_2_hash;
undef @arr_2_hash{@arr_2};
grep !exists $arr_2_hash{$_}, @arr_1;
}
};
cmpthese(-3, $cmp);
I do like the package approach more though, because there's no reference to a hash of refences to subs, which probably is quite unsettling for beginners. And even if they don't understand how it works, it's surely better if they at least understand the syntax to use it, instead of just copying an exemple with even weirder concepts than those they already have trouble understanding.
If you go for the package solution, you might as well have it use parent YourBenchmark, so that you can call CMP->benchmark(-5);. And you may want to add filtering of some sort, because one might want to call a function imported from another package inside of the functions that should be benchmarked. I'm sure someone could have come up with a function using something out of List::AllUtils for the exemple you gave. Edit: and you already said that with using the attributes as the tool for selecting the benchmarked subs, my bad.
-
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.