Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam authored May 12, 2021
1 parent 8898eda commit e0e3e07
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,47 +169,45 @@ Implicit multiplication - `xy` is identical to `x*y`, `3x` is identical to `3*x`

Performance is one of the largest benefits of using Crunch. It is designed to be extremely performant, and lives up to that expectation. For cases where you need to perform a lot of evaluations quickly from a string-compiled mathematical expression, Crunch is the best option.

Here I will compare the runtimes of Crunch against another similar library, [EvalEx](https://github.com/uklimaschewski/EvalEx). I will compare both compilation times and evaluation times.
Here I will compare the runtimes of Crunch against three similar librararies: [EvalEx](https://github.com/uklimaschewski/EvalEx), [exp4j](https://github.com/fasseg/exp4j), and [java.math.expression.parser](https://github.com/sbesada/java.math.expression.parser). I will compare both compilation times and evaluation times.

CPU: AMD Ryzen 5 2600

## Compilation

Expression: `3*5`

| Crunch | EvalEx |
|:-------|-------:|
|22μs |502μs |
| Crunch | EvalEx | exp4j | java.math.expression.parser |
|:-------|--------|--------|-------|
|22μs |502μs |64μs |48μs|

Expression: `6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4 + 6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4 + 6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4 + 6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4`

| Crunch | EvalEx |
|:-------|-------:|
|532μs |559μs |
| Crunch | EvalEx | exp4j | java.math.expression.parser |
|:-------|--------|-------|---------|
|532μs |559μs |811μs |687μs|

As you can see, Crunch is much faster at compiling short expressions, and stacks up pretty evenly with long expressions. EvalEx is actually misleading here, though, as it doesn't compile an expression when its `Expression` class is initialized - it doesn't touch the string at all until evaluate is called. Even so, Crunch can compile this long expression in the time it takes EvalEx to initialize its operators without even touching the string.
As you can see, Crunch is much faster at compiling short expressions, and stacks up pretty evenly with long expressions. EvalEx is actually misleading here, though, as it doesn't compile an expression when its `Expression` class is initialized - it doesn't touch the string at all until evaluate is called. Even so, Crunch can compile this long expression in the time it takes EvalEx to initialize its operators without even touching the string. java.math.expression.parser and exp4j are both roughly comparable to Crunch for compilation times here, moreso for longer expressions.

## Evaluation

The times shown below are for 10,000 evaluations.

Expression: `6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4 + 6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4 + 6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4 + 6.5*7.8^2.3 + (3.5^3+7/2)^3 -(5*4/(2-3))*4`

| Crunch | EvalEx |
|:-------|-------:|
|504μs |24,621μs|
| Crunch | EvalEx | exp4j | java.math.expression.parser |
|:-------|--------|--------|----------|
|504μs |24,621μs|30,778μs|279,162μs |

Crunch has an unfair advantage in this scenario, though, since it simplifies expressions using only constants where possible. Since this expression is made of only constants, Crunch will reduce it to a single constant value rather than running through all of the values every time.
Crunch has an unfair advantage in this scenario, though, since it simplifies expressions using only constants where possible. Since this expression is made of only constants, Crunch will reduce it to a single constant value rather than running through all of the values every time. java.math.expression.parser doesn't seem to have a way to compile an expression, so it essentially has to start from scratch for each evaluation.

So now for a more realistic test: Expressions using variables.
So now for a more realistic test: A simple expression using a variable.

Expression: `3*$1` (for EvalEx, `3*x`)
Expression: `3*x`


| Crunch | EvalEx |
|:-------|-------:|
|1,315μs |31,654μs|
| Crunch | EvalEx | exp4j | java.math.expression.parser |
|:-------|--------|-------|-----------|
|1,315μs |31,654μs|9,495μs|62,581μs |

In both cases, Crunch is about 25-50 times faster for evaluation.

There is another library, [java.math.expression.parser](https://github.com/sbesada/java.math.expression.parser), which also serves a similar purpose, but was not included in this benchmark because rather than compiling and then evaluating expressions, it evaluates them as strings once. This method will obviously be slower than compiling an expression for multiple evaluations, though according to its own benchmarks, this library is still slower to evaluate a long expression a single time than Crunch is to compile and evaluate it.
In both cases, Crunch is about 25-50 times faster than EvalEx for evaluation, and 7-60 times faster than exp4j. There is no reasonable comparison to java.math.expression.parser because it cannot truly compile expressions.

0 comments on commit e0e3e07

Please sign in to comment.