I have and it's not. Or, at least, it's not faster. But it's also more general purpose.
my $op = '+';
my $add_5_things = arbitrary_operator_maker($op, 5);
my @list_5 = (1..5);
timethese(1000000, {
'add_5_things elements cached' => sub {
$add_5_things->(@list_5);
},
'add_5_things elements dispatched' => sub {
my $add_5_things = arbitrary_operator_maker($op, 5);
$add_5_things->(@list_5);
},
'multi_add 5 elements' => sub {
multi_add(@list_5);
},
'reduce' => sub {
reduce { $a + $b} @list_5;
}
});
Benchmark: timing 1000000 iterations of add_5_things elements cached,
+add_5_things elements dispatched, multi_add 5 elements, reduce...
add_5_things elements cached: 0 wallclock secs ( 0.99 usr + 0.00 sys
+ = 0.99 CPU) @ 1010101.01/s (n=1000000)
add_5_things elements dispatched: 2 wallclock secs ( 2.94 usr + -0.00
+ sys = 2.94 CPU) @ 340136.05/s (n=1000000)
multi_add 5 elements: 2 wallclock secs ( 2.52 usr + 0.01 sys = 2.53
+ CPU) @ 395256.92/s (n=1000000)
reduce: 4 wallclock secs ( 3.24 usr + 0.01 sys = 3.25 CPU) @ 30
+7692.31/s (n=1000000)
Or, with a bigger set:
my $op = '+';
my $add_500_things = arbitrary_operator_maker($op, 500);
my @list_500 = (1..500);
timethese(10000, {
'add_500_things elements cached' => sub {
$add_500_things->(@list_500);
},
'add_500_things elements dispatched' => sub {
my $add_500_things = arbitrary_operator_maker($op, 500);
$add_500_things->(@list_500);
},
'multi_add 5 elements' => sub {
multi_add(@list_500);
},
'reduce' => sub {
reduce { $a + $b} @list_500;
}
});
Benchmark: timing 10000 iterations of add_500_things elements cached,
+add_500_things elements dispatched, multi_add 5 elements, reduce...
add_500_things elements cached: 1 wallclock secs ( 0.72 usr + 0.00 s
+ys = 0.72 CPU) @ 13888.89/s (n=10000)
add_500_things elements dispatched: 0 wallclock secs ( 0.74 usr + 0.
+00 sys = 0.74 CPU) @ 13513.51/s (n=10000)
multi_add 5 elements: 0 wallclock secs ( 1.03 usr + 0.00 sys = 1.03
+ CPU) @ 9708.74/s (n=10000)
reduce: 1 wallclock secs ( 1.18 usr + 0.00 sys = 1.18 CPU) @ 84
+74.58/s (n=10000)
Of course, it's also much more general purpose than the arbitrary_operator_maker is, which is highly specialized. But for these tests, though, reduce was the slowest of the bunch. Benchmarks are your friend.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|