Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: The arbitrary operator maker

by samtregar (Abbot)
on Nov 22, 2006 at 20:30 UTC ( [id://585606]=note: print w/replies, xml ) Need Help??


in reply to The arbitrary operator maker

Have you seen List::Util's reduce routine? It's supposed to be pretty fast.

-sam

Replies are listed 'Best First'.
Re^2: The arbitrary operator maker
by jimt (Chaplain) on Nov 22, 2006 at 20:44 UTC

    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.

      In your final comparison between reduce and other operations you note that being specialized allows you to be faster. Sure. Ok. But you didn't buy much with that.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        It is faster and doesn't use XS. Of the two, the latter I find to be a bigger advantage.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://585606]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found