|
Equation Parsers (July 2007)
Equation Parsers, also known as math parsers or expression
evaluators, are used to solve equations entered as a text string at run time,
rather than hardcoding the equation into the application. The parsers
interpret (parse) the equation, then use variable values to
evaluate the equation. Parsers are typically distributed as DLLs, OCXs,
or source code.
Of the 20+ available equation parsers, there are about twice as many
commercial as freeware versions. Commercial parsers are generally easier
to insert into applications. Freeware excelled in performance, where 3 of
the top 4 speeds were turned in by freeware parsers.
In addition to dedicated parsers, equation parsing capabilities are
also available in more generalized software, such embeddable scripting
language libraries and ActiveX spreadsheet components.
Review Results
The basics of all parsers are pretty much the same - add a DLL/OCX to an application,
define custom functions/variables, define the equation to solve, set variable values
and then evaluate the equation. Parsers vary greatly in the number of built-in
functions, documentation completeness, availability of sample insertion code examples,
and speed. Even though most parsers allow the user to create custom functions,
a greater number of built-in functions makes implementation simpler on the programmer.
All of the parsers were fairly easy to implement, with most requiring less than 10
lines of code (which is good, because documentation on implementation code is not a
strong point for most parser authors). The greatest difference between parsers was
in speed, with over 1000X difference between the slowest and fastest parsers.
From the full list of equation parsers, here are
the four parsers with the highest speeds:
- MTParser 2.5 Mips, $0, 2007 (batch mode available for greater speeds)
- uCalc 2.5 Mips, $0, 2007 (next version to be commercial, $600)
- muParser 2.5 Mips, $0, 2007
- CioinaEval 2.1 Mips, $0, 2003
Notes: 1. The website of a top performer,
EquTranslator (2.8 Mips, $495, 2007),
has recently gone offline.
2. muParser data from newly released, VB6-compatible DLL (July 2007)
3. MTParser batch data from newly released, VB6-compatible batch mode DLL routine (July 2007)
Bottom Line: The differences between the top three performers (uCalc,
MTParser, and muParser) are very minimal. Although uCalc is freeware, it is also
part of a line of commercial products, so many users will want the assurance of
responsive support and continued development that it represents. Both MTParser
and muParser are mature freeware products with active online support from their
authors, so users who prefer freeware can feel comfortable using either of these.
Note that MTParser offers a batch mode for transferring variable values. This mode
gives 10%-140% speed improvements over competing parsers, depending on the complexity of
the equation. If you can use the batch mode, then MTParser offers you the fastest
speeds available, particularly on equations of lower complexity.
Additional Review Comments
Speed (measured in Million iterations per second) testing was done on a Gateway Intel
Core 2 Duo E6400 PC (2.11GHz), using a compiled VB6 test program to test
parsers on five equations of varying complexity. The results above represent the most
complex equation tested, "abs(sin(sqrt(x^2+y^2))*255)". At lower complexities,
speeds were 2X-3X faster, as seen in the complete speed results.
There are two speeds measurements of interest - time to first equation evaluation
and speed of iterating subsequent evaluations. The latter is the most common
usage of equation parsers and is the speed measured in this review.
This review focused on parsers capable of being used in multiple languages.
Other parsers that were reviewed include source
code (VB6, Delphi, and Java), ActiveX objects (commercial spreadsheet OCXs),
the Eval() function (from JavaScript and VBA), and general purpose embeddable
scripting languages. Most of these other parsers offer lower speed solutions,
but provide other advantanges that a programmer might find useful.
I've also put together an
Excel spreadsheet that provides all the information, and more, found on this
page. I welcome any feedback from users on additional information they
need to evaluate parsers for their needs.
One question not answered in this review is performance of the parsers in other
languages. As an update to this page, I am working on testing the parsers
in VC++ and C#.
Key Parser Issues
In addition to speed, there are several factors user will want to consider in
selecting a parser.
- Speed
The operators/functions used in an equation can make a huge difference
in performance speeds, between competing parsers or even within an
individual parser. When speed is critical, be sure to try out equations
that are representative of your needs to make sure you get the parser best
suited to providing you with optimum performance. For example,
exponential and trigonometric functions are often handled much slower
than other more common functions. For my review I use an equation of
moderate complexity - "abs(sin(sqr(x^2+y^2))*255)".
Note that there are two parser speed parameters of interest, the time
to compile an equation and the time to evaluate an equation.
For applications needing to evaluate a single equation millions of times,
typically with new variable values on each iteration, compilation can significantly
reduce the time to complete all iterations, even though the first equation
is slower due to compilation time. My review did not measure the speed of
compilation, which could be important in applications which need to
compile/evaluate many different equations. I will likely expand the review
later to cover this issue.
In my speed tests, which combine compilation and evaluation,
parser speeds ranged from a very low 0.002 Mips (million
iterations per second) to a high of over 10 Mips.
The highest speeds are achieved with only the simplest of equations.
As equation complexity grows, the highest performing parsers generally
achieved 2-3 Mips results.
For evaluating an equation with a single set of variables, even the
slowest parsers work well in most applications. Even at 0.002 Mips
the user will interpret a single iteration as taking place instantly.
However, for compiling an equation once only, then performing millions of
iterations of variable assignment and equation evaluation, only the
highest performing parsers (2-3 Mips) will can begin to give users a sense of speed.
For example, to modify a single 1280x1024 image "instantly" (in about 1/4
second), a parser speed of about 5 Mips is needed. This means that
the best parsers can approach speeds that users will consider fast.
For limited animation speed/resolution, such as 15fps of 640x480 images,
an overall speed of 5 Mips is required - close to the capabilities of the
best parsers. However, at 30 fps animation of 1280x1024 images, a parser
speed of 40 Mips is needed - well beyond the speeds available from the parsers
available today.
- Documentation
There were a few parsers which provided lengthy documentation. But
as a group parser documentation is poor - particularly lacking in
summary/overview information and in providing adequately commented
sample source code in all of the languages the parser supports.
VB source code was particularly hard to find.
I was especially disappointed to find that almost none of the parsers provided
a complete, convenient, online summary/listing/definition/syntax of the parser
objects and their interface (properties/methods).
The good news is that most parsers require only a few lines of code to
implement. So, the lack of documentation, while frustrating, is not that
hard to covercome.
- Application Language
The language used to write the application which uses the parser can also
be a factor in the parser performance. All of my testing was done using
the parser DLLs/OCXs in a VB test program, but parsers do not always perform
the same in each of the most popular languages (C++, Delphi, VB, VB.NET). Unfortunately,
the authors do not usually address these performance issues. If you have the option of
using more than one language for your application, you'll want to consider
running tests to make sure you know how well the parser performs in your language.
- Author Support / Active Development
Before you commit your product to using a parser you'll want to know how mature
the product is and whether bug fixes/improvements will be available. Almost 50%
of the commercial parsers have been updated in the last year, whereas only about 20% of
the freeware parsers have had updates with the last year. In both categories,
however, author support continues to be available for most of the parsers, especially
the top performing parsers.
- License Limitations
Most programmers want to pay a single fee which allows them to freely
use the parser in any/all of their applications. This type of license
is generally provided, but there are some very specific exceptions. Be
sure to read the parser licensing agreement to make sure it fits your needs.
- Supported Functions
Pretty much all parsers handle a common set of core operators and functions.
However, there are some parser differences which might drives a user's choice
- such as extended precision, complex numbers, or other math functions
specific to a particular scientific discipline. Most parsers also offer
the ability to add custom operators/functions.
A side-by-side summary of
parser-supported functions
is available online.
Full List of Equation Parsers
The follow parsers were reviewed. Freeware programs are listed in alphabetical order
and commercial programs are listed from least to most expensive.
The speed column is in million iterations per second (Mips). Speed tests were performed
on my Gateway E6400 Dual Core Pentium PC. This table has only the speed
results for the equation "abs(sin(sqr(x^2+y^2))*255)",
but speed results on
all five test equations are presented further down this page.
If you're aware of a product I've missed, please let me know. Or, if you're aware
of an aspect of a program that you think I should consider in selecting a program,
feel free to let me know that too.
Notes:
(1) uCalc v2.9 is currently a free product. v3.0 will return
(2) EffObjects Equation Parser Library trial was only available as
a compiled EXE, which I used to get these test results.
(3) Precision Expression Evaluator trial gives a popup for each
iteration in a loop, preventing testing of million+ loops. However
the author provided a temporary license to use for purposes of this review.
(4) USPExpress trial version provides limited functionality,
so the speed test had to be run on a different equation.
Complete Speed Test Results
The following code snippet shows the Visual Basic source code used for the
hardcode test. The code is commented to show where code is added to get
parser-specific results.
'parser initialization/compile code goes here
For y = 1 To 2000
For x = 1 To 2000
'parser variable assignment / equation evaluation goes here
Result = abs(Sin(Sqr(x^2 + y^2))*255) 'hardcoded example
Next x
Next y
I used code similar to this, along with parser-specific code, to generate the
parser speed test results. Five equations of varying complexity were used:
- x*2+y*2
- sin(x)+sin(y)
- x^2+y^2
- abs(sin(sqr(x^2+y^2))*255)
- abs(sin(sqr(x*x+y*y))*255)
And here are the results of the speed tests for each parser and for each equation.
| Parsers | Eq.1 | Eq.2 | Eq.3 | Eq.4 | Eq.5 | Comments
|
|---|
| Freeware----------------
| | dotMath Library | tbd | tbd | tbd | tbd | tbd | .NET test needed
| | Expression Evaluator | tbd | tbd | tbd | tbd | tbd | .NET test needed
| | Lua Scripting Language | 4.4 | 1.9 | 2.4 | 1.2 | 1.4
| | MTParser | 8.6 | 4.9 | 3.2 | 2.5 | 4.9
| | MTParser (batch) | 17.0 | 6.7 | 3.9 | 2.8 | 6.6
| | muParser | 10.3 | 5.3 | 3.4 | 2.5 | 4.9
| | uCalc | 7.1 | 4.3 | 3.7 | 2.6 | 3.4
| | Windows Script Control | 0.026 | 0.026 | 0.026 | 0.024 | 0.024
| |
|
| Commercial-------------
| | AxEval | 0.0008 | 0.0002 | 0.0001 | 0.0001 | 0.0001
| | bcParser | 0.84 | 0.67 | 0.67 | 0.55 | 0.67
| | TFunctionParser | 0.22 | 0.22 | 0.22 | 0.20 | 0.20
| | ecMathExpr | 0.11 | 0.10 | 0.10 | 0.09 | 0.08
| | Equation Parser Library | 1.2 | 1.0 | 0.86 | 0.47 | 0.53 | author EXE used
| | CioinaEval | 4.1 | 3.0 | 2.7 | 2.1 | 2.9
| | Precision Expr Evaluator | 0.21 | 0.21 | 0.21 | 0.16 | 0.16
| | #Calculation Component | 0.0025 | 0.0018 | 0.0024 | 0.0014 | 0.0013
| | USPExpress | 4.4 | 2.3 | ~1.8 | ~1.2 | 1.4 | trial version limitations
| | EquTranslator | 12.8 | 5.7 | 3.9 | 2.8 | 5.2
| | uCalc | 7.1 | 4.3 | 3.7 | 2.6 | 3.4
| |
|
| Web Page Solutions-------
| | JavaScript Eval() | 0.020 | 0.020 | 0.020 | 0.015 | 0.016
| | JavaScript non-Eval() | tbd | tbd | tbd | tbd | tbd
| |
|
| VB6-Only Solutions-------
| | clsMathParser | 1.43 | 1.30 | 0.87 | 0.56 | 0.74
|
Other Parsers
The parsers discussed above are capable of being used in multiple languages.
There are other parsers options available. These include uncompiled source
code which supports a single language (VB.NET, Delphi, JavaScript or Java).
Equation parsing capabilities are also available as secondary features of ActiveX components
(such as spreadsheet components) and scripting engines. This section discusses these
other types of parser options. I include speed test results, where available.
- VB6-only Parsers (source code)
These parsers are source code which may be inserted into a VB6 program to provide
parsers functions. Their speeds will be low, but still fast enough for many
applications. Plus, the offer the flexibility of being able to modify the code
for improvements and bug fixes.
Note: The speed quoted for the Simple Expression Parser is for the simplest test equation.
I was unable to get the parser to work on the other, more complicated test equations. Also,
Eval for VB trial was only available as a compiled EXE which could not loop, so I had to
use a custom EXE supplied by the author.
- Delphi-only Parsers (source code)
In late '90s a number of Delphi-specific (Pascal) parsers appeared. Most of these seem
to be derivations of one another, starting with the original work by Hoffmeister. The
documentation on these indicate that good speeds were attained, but I've seen no specific
numbers and I do not have Delphi to test the approaches.
- Java-only Parsers (source code)
These solutions provide equation parsing capabilities for web pages. I've yet to test these
for performance.
- JFormula by JAPISoft ($733, 2006)
- JEP by Singular Systems ($350, 2007)
- Math Parser by Hussm Al-Mulhim ($0, 2005)
- Excel Objects
If your programming language supports inclusion of an Excel object, you can use
Excel's capability to enter and evaluate equations.
- ActiveX Spreadsheets
There are commerical spreadsheet ActiveX components which provide Excel-like
cability to enter and evaluate equations. While I did not include such components
in this review, it's worth noting that they exist.
I did test one such component, Formula One, which I've used for years. At 0.3 Mips,
its performance was low, but if you already own the tool then the parser capability
is free and the product is very mature.
- Built-In Eval() Function
Many languages or scripting engines have a builtin function, usually called
Eval(). The function is usually not a high performing parser, but when available
can be very useful while avoiding the need for embedding a third party parser.
- JavaScript Eval() Function
Using the Eval() function built into JavaScript you can add equation
parser functionality to your web pages. The problem with using the Eval()
function is that the equation has to be entered using JavaScript syntax,
which is not always the same as a user is used to seeing. For example,
to x^2 must be entered as Math.pow(x,2), or sin(x)
must be entered as Math.sin(x). By my tests, speed performance
is in the 0.2 Mips range.
Here are some online examples using this approach. You'll have to use
your browser's View - Page Source menu option to see the JavaScript code.
- JavaScript - without Eval() Function
Using JavaScript programming it's also possible to create a parser
(without using the Eval function) which allows users to enter
equations in more typical format. Source code would be similar in
concept to the freeware/commercial parsers discussed elsewere in
this review.
Here are some online examples using this approach. You'll have to use
your browser's View - Page Source menu option to see the JavaScript code.
- VBA
VBA is a scripting language built into several Microsoft products. It
provides an Eval() function, which is a built-in equation parser.
You can declare a link to the vba.dll to add access to the Eval() function
from your own applications. I've seen the code available on the web
but have not evaluated it myself.
- Scripting Engines
There are a number of products available, designed to allow programmers to add
scripting capabilities to their applications. The Microsoft Windows Script Control
and the Lua Scripting Language products (part of this review) are examples. Both
of these can be used to provide scripting capabilities to users. The Microsoft
Windows Script Control parsing speed is very slow (0.024 Mips), whereas the Lua Scripting Language
DLL provides relatively fast (0.88 Mips) parsing capabilities.
Review Notes
While collecting information about the parsers, I've taken the following
freestyle notes on most, but not all, parsers (placeholders had been added
to remind me to adds some comments as soon as possible).
Parser Insertion Code & Include Files
This section links to the source code used to implement the parsers
and summarizes the files (included in the parser installation package)
that had to be included in the VB test program.
The VB6 source code used for testing is
available. It includes an example of the minimum code required to implement
the parser and also includes the complete VB6 code used in the speed test project
(not including the parser DLLs/OCXs or author-provided .BAS/.CLS files).
If anyone can see where the code I used in any way limited the parser's
performance, please let me know - including your suggestions on how to
get increased performance.
- clsMathParser
- clsmathparser.cls and mMathSpecFun.bas (102K, 51K)
- MTParser
- mtparsercom.dll (272K)
- muParser
- muparser.dll (132K)
- uCalc
- ucalcE29.dll, ucalcF29.dll and ucalcvb,bas (40K, 109K, 35K)
- AxEval
- axeval.dll (172K)
- bcParser
- bcParser.dll (140K)
- Cioina
- cioina.dll and cioina.bas (220K, 16K)
- Precision Expression Evaluator
- expressionevaluator.ocx (56K)
- #Calculation Components
- sharpcalculation.ocx (107K)
- USPExpressParser
- uspexpressparser.dll (276K)
- EquTranslator
- equtranslator.dll and equtranslator.bas (124K, 2K)
- Simple Expression Parser
- expval.bas (18K)
- Eval for VB/VBCE
- uses .BAS files, but not available from author
Author Recognition
I'd like to acknowledge the support of several of the equation parser authors.
- Daniel Corbier (uCalc)
for his release of uCalc 2.9 as freeware and his tireless answering of
my questions, and for not getting mad at me for a string of recommendations
on his product and web site
- Ingo Bert (muParser)
for providing a re-compiled version of muParser compatible with VB6 and
for supporting speed improvements for VB6 users
- Mathieu Jacques (MTParser)
for his interest in providing VB6 speed improvements of MTParser
- Tuomas Salste (Eval for VB/VBCE)
providing a compiled test version of his source code product, and for
his insight on iterating on equation, rather than variable, changes
- Peter Bessonov (AxEval)
answering questions and for letting me know about the Lua Embeddable Scripting Language
- Jianhua Li (#Calculation Component)
For the additional VB6 source code examples
Other URLs
I found several other URLs which have information on equation parsers.
I didn't use information from these sites, but they may be of use to you
in gaining a greater understanding of parser code/concepts.
I especially liked this short summary of
parser concepts.
Other interesting sites include:
|