Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: RFC: Junction.pm

by fireartist (Chaplain)
on May 02, 2005 at 20:44 UTC ( #453406=note: print w/replies, xml ) Need Help??


in reply to RFC: Junction.pm

Hmm, I'm undecided about this. I've had a look over Quantum::Superpositions now, and it's got a lot of cool stuff in it. However, I see my approach as mostly syntactic sugar to replace things like:

if ($param eq 'this' || $param eq 'that' || $param eq 'the other') { ... }
with
if ($param eq any('this', 'that', 'the other') { ... }
which Quantum::Superpositions seems a bit overkill for.

Taking a hint from hardburn above, I did some benchmarks (below) and there is a significant runtime difference, between using my code and Quantum::Superpositions'.

One feature from Quantum::Superpositions that I think would be worth adding, is

$result = any(1,2,3) * 2; if ($result == 4) {...}
which would be trivial to add.

Tanktalus asked above about if (all(@x) eq any(@y) {...}. I can see how useful that would be, but would have to experiment to find a way to implement it without getting as big as Q::S, while not making the code too ugly.

Would Junction::Simple be a more fitting name? (I'm very hesitant to claim a top-name space)

Benchmark timings...

$ /usr/bin/perl ./benchmark.pl all: numeric (few) Rate Quantum-Superpositions Ju +nction Quantum-Superpositions 1842/s -- + -94% Junction 30303/s 1545% + -- all: numeric (lots) Rate Quantum-Superpositions Jun +ction Quantum-Superpositions 1364/s -- + -44% Junction 2433/s 78% + -- all: string Rate Quantum-Superpositions Jun +ction Quantum-Superpositions 1689/s -- + -79% Junction 8000/s 374% + -- any: numeric (few) Rate Quantum-Superpositions Ju +nction Quantum-Superpositions 747/s -- + -97% Junction 28571/s 3725% + -- any: numeric (lots) Rate Quantum-Superpositions Jun +ction Quantum-Superpositions 37.9/s -- + -97% Junction 1429/s 3672% + -- any: string Rate Quantum-Superpositions Jun +ction Quantum-Superpositions 317/s -- + -95% Junction 6422/s 1928% + --

And here's the test code:

#!/usr/bin/perl use strict; use warnings; use Junction qw/ xall xany /; use Quantum::Superpositions qw/ all any /; use Benchmark ':all'; print "all: numeric (few)\n"; cmpthese(30_000,{ Junction => sub { 1 if xall(1..5) == 2; }, 'Quantum-Superpositions' => sub { 1 if all(1..5) == 2; }, }); print "\nall: numeric (lots)\n"; cmpthese(30_000,{ Junction => sub { 1 if xall(1..300) == 250; }, 'Quantum-Superpositions' => sub { 1 if all(1..300) == 250; }, }); print "\nall: string\n"; cmpthese(10_000,{ Junction => sub { 1 if xall('a'..'z') eq 'z'; }, 'Quantum-Superpositions' => sub { 1 if all('a'..'z') eq 'z'; }, }); print "\nany: numeric (few)\n"; cmpthese(30_000,{ Junction => sub { 1 if xany(1..5) == 2; }, 'Quantum-Superpositions' => sub { 1 if any(1..5) == 2; }, }); print "\nany: numeric (lots)\n"; cmpthese(2_000,{ Junction => sub { 1 if xany(1..300) == 250; }, 'Quantum-Superpositions' => sub { 1 if any(1..300) == 250; }, }); print "\nany: string\n"; cmpthese(7_000,{ Junction => sub { 1 if xany('a'..'z') eq 'z'; }, 'Quantum-Superpositions' => sub { 1 if any('a'..'z') eq 'z'; }, });

Replies are listed 'Best First'.
Re^2: RFC: Junction.pm
by eric256 (Parson) on May 02, 2005 at 21:05 UTC

    Tanktalus asked above about if (all(@x) eq any(@y) {...}. I can see how useful that would be, but would have to experiment to find a way to implement it without getting as big as Q::S, while not making the code too ugly.

    Actualy it is not all the difficult. Realy once you have overloaded the operators perl manages most of the extra magic to make that work without bloating your code. The code i linked to before is under 60 lines and handles that as well as embeding different any() and all() objects inside each other. For instance it handles print "YEA" if all( any(1..5), any(1..10), any(1..20)) > all(2..4); as expected.

    /me is still dieing to see how you approached the problem.


    ___________
    Eric Hodges

      I've uploaded it to cpan as Perl6::Junction.

      It's implemented more simply than yours (plain subs / no eval), which means that it's more lines of code, but in my benchmarks ran around 500% faster.

      See my reply to Tanktalus above, for more details on what it supports.

        Actualy the speed difference has nothing to do with the evals. The evals are all done at compile time to do the overloading without 700 lines of code ;).

        The difference is that you short ccircuit the logic operation so that it stops as soon as it knows the anser. Mine collects data and reports what items caused the failure.

        A quick less than scientific benchmark using short circuiting speeds my code up to the point where yours is 350% faster still.. Which isn't bad but since the same benchmark was showing yours 1277% faster a second ago its not a bad speed up.


        ___________
        Eric Hodges
        Aren't the math operators something like:
        sub do_<op> { my ($self, $other, $switch) = @_; @$self = map { $switch ? ($other <op> $_) : ($_ <op> $other) } @$self; return $self; }

        The Perfect is the Enemy of the Good.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-01-23 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?