Math-Expression-Evaluator

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    optimization nearly doubles the execution speed. The compiled and the
    optimized-and-then-compiled versions are both much faster.

    With this example expression the optimization prior to compilation pays
    off if you evaluate it more than 1000 times. But even if you call it
    "10**5" times the optimized and compiled version is only 3% faster than
    the directly compiled one (mostly due to perl's overhead for method
    calls).

    So to summarize you should compile your expresions, and if you have
    really many iterations it might pay off to optimize it first (or to
    write your program in C instead ;-).

BUGS AND LIMITATIONS
    *   Modulo operator produces an unnecessary big AST, making it
        relatively slow

INTERNALS
    The AST can be accessed as "$obj-"{ast}>. Its structure is described in
    Math::Expression::Evaluator::Parser (or you can use Data::Dumper to
    figure it out for yourself). Note that the exact form of the AST is

benchmark.pl  view on Meta::CPAN

use strict;
use warnings;

use lib 'lib';
use Carp qw(confess);
use Benchmark qw(cmpthese);
use Math::Expression::Evaluator;
use Data::Dumper;

my $statement = '2 + a + 5 + (3+4)';
my $iterations = $ARGV[0] || 200;

sub with_optimize {
    my $m = Math::Expression::Evaluator->new($statement);
    $m->optimize;
    for (1..$iterations){
        $m->val({a => $_});
    }
}

sub no_optimize {
    my $m = Math::Expression::Evaluator->new($statement);
    for (1..$iterations){
        $m->val({a => $_});
    }
}

sub compiled {
    my $m = Math::Expression::Evaluator->new($statement);
    my $c = $m->compiled();
    for (1..$iterations){
        $c->({a => $_});
    }
}

sub opt_compiled {
    my $m = Math::Expression::Evaluator->new($statement);
    $m->optimize();
    my $c = $m->compiled();
    for (1..$iterations){
        $c->({a => $_});
    }
}


my %tests = (
        optimize       => \&with_optimize,
        no_optimize    => \&no_optimize,
        compiled       => \&compiled,
        opt_compiled   => \&opt_compiled,

lib/Math/Expression/Evaluator.pm  view on Meta::CPAN

As you can see, the non-optimized version is painfully slow, optimization
nearly doubles the execution speed. The compiled and the 
optimized-and-then-compiled versions are both much faster.

With this example expression the optimization prior to compilation pays off
if you evaluate it more than 1000 times. But even if you call it C<10**5>
times the optimized and compiled version is only 3% faster than the directly
compiled one (mostly due to perl's overhead for method calls).

So to summarize you should compile your expresions, and if you have really
many iterations it might pay off to optimize it first (or to write your
program in C instead ;-).

=head1 BUGS AND LIMITATIONS

=over 4

=item *

Modulo operator produces an unnecessary big AST, making it relatively slow



( run in 2.819 seconds using v1.01-cache-2.11-cpan-71847e10f99 )