1Math::Expression::EvaluUasteorr:C:oOnpttriimbiuzteerdM(a3Pt)ehr:l:EDxopcruemsesnitoant:i:oEnvaluator::Optimizer(3)
2
3
4
6 Math::Expression::Evaluator::Optimizer - Optimize
7 Math::Expression::Evaluator ASTs
8
10 use Math::Expression::Evaluator;
11 my $m = Math::Expression::Evaluator->new("2 + 4*f");
12 $m->optimize();
13 for (0..100){
14 print $m->val({f => $_}), "\n";
15 }
16
18 Math::Expression::Evaluator::Optimizer performs simple optimizations on
19 the abstract syntax tree from Math::Expression::Evaluator.
20
21 You should not use this module directly, but interface it via
22 Math::Expression::Evaluator.
23
24 The following optimizations are implemented:
25
26 • Constant sub expressions: "variable + 3 * 4" is simplfied to
27 "variable + 12".
28
29 • Joining of constants in mixed constant/variable expressions: "2 +
30 var + 3" is simplified to "var + 5". Works only with sums and
31 products (but internally a "2 - 3 + x" is represented as "2 + (-3)
32 + x", so it actually works with differences and divisions as well).
33
34 • Flattening of nested sub expression: "a * (3 * b)" is flattened
35 into "a * 3 * b". Currently this is done before any other
36 optimization and not repeated.
37
39 "optimize()" currently takes two full loops through the AST, copying
40 and recreating it. If you execute "val()" only once, calling
41 "optimize()" is in fact a performance loss.
42
43 If the expression is optimizable, and you execute it $n times, you
44 usually have a net gain over unoptimized execution if "$n > 15".
45
46 Of course that value depends on the complexity of the expression, and
47 how well it can be reduced by the implemented optimizations.
48
49 Your best is to always benchmark what you do. Most of the time the
50 compiled version returned by "->compiled" is much faster than the
51 optimized (and not compiled) form.
52
53
54
55perl v5.32.1 2021M-a0t1h-:2:7Expression::Evaluator::Optimizer(3)