05-18-2018 03:49 PM
@Porter wrote:
My solution to this problem was to use muparser: http://beltoforion.de/article.php?a=muparser
You can add custom functions and operators if needed.
I posted an API for LabVIEW here: https://lavag.org/files/file/295-lv-muparser/
This is pretty cool - I have implemented my own purely in LabVIEW, which uses look up tables for variable access - but its really slow - about .5ms for a pretty small expression. Do you have any metrics on performance?
05-19-2018 12:11 PM
I have included some benchmarking and test cases in the development version: https://github.com/rfporter/LV-muParser
I recall that the performance was a bit better than the native LabVIEW formula parser.
Bulk mode evaluation is where you can get a major gain in performance over the native version.
Also, you save about 10 minutes when building an executable if you can avoid including the ni_gmath.lvlib (where the native formula parser resides).
05-20-2018 03:03 PM
As I recall, the native LabVIEW formula parser has a problem. Each evaluation, all input variables are evaluated. This means that the more variables you have, the slower it gets. This is really something that you want to be done up front. So if the native parser is slower then yours, simply add more variables until it's slower then yours .
Another problem with the native parser (IIRC) is that only the first three characters are used to index the variables. Probably a 20 year old design decision, but really not good.
05-21-2018 06:10 AM - edited 05-21-2018 06:15 AM
I did a long time ago a completely reworked version of the formula parser here.
It's basically a complete rewrite from scratch up and it was able to perform pretty quick back in LabVIEW 6.0. I used this as a base in several applications since, where I did improve on the original design, but that is to intertwined with the actual application logic that I could post it now. As it was created in LabVIEW 5.1 days it does not use any classes and some of the architecture is by nowadays standards a bit arcane.
One of the features is that it has a parser part which translates the actual formula into a stack based UPN notation tree, and the actual expression evalution part which calculates the formula result. It supports arbitrary variable names with the restriction that a variable name needs to start with an alphabetic character and then can contain any alphanumeric character and the underscore as many times as you wish.
It still is a useful starting point for your own work, although various optimization work in the LabVIEW compiler itself has made the original huge performance difference between the NI formula parser and mine a lot smaller.
05-22-2018 10:15 AM
Here are some numbers for comparison.
For the expression "1*x^2+2*x+2" parsing once then evaluating over 10000 values of "x".
Average time to evaluate:
LabVIEW Formula Parser: 1.55 µs
muParser LabVIEW API: 0.49 µs
muParser LabVIEW API (bulk mode): 0.02 µs
For the expression "1*x^2+2*x+2" parsing and evaluating for 1 value of "x" 10000 times.
Average time to parse and evaluate:
LabVIEW Formula Parser: 186.1 µs
muParser LabVIEW API: 158.8 µs
05-22-2018 10:21 AM
Rolf, any chance of posting a copy of your parser for LV 8+?
05-22-2018 02:38 PM
I haven't looked at the current NI implementation but running a similar test to yours on my machine with my libraray (LV 8.0, Windows 10, Core i7 2GHz) seems to indicate that they must have considerably reworked something in there as the timing on my library seem pretty similar to the one you quote for the NI library.
1000000 * evaluation only = 1.25 us per iteration
10000 * (parsing + evalution) = 104 us per iteration
There is some jitter when running it multiple times but the values stay fairly consistent.
Included is the 8.0 compiled version of the VIs..
05-22-2018 03:10 PM - edited 05-22-2018 03:17 PM
For completeness, with Rolf's expression parser.
On LV 2015, Windows 10, core i5, 2.9GHz. Expression "1*x^2+2*x+2"
10000 * evaluation only, average time per iteration
LabVIEW Formula Parser: 1.55 µs
muParser LabVIEW API: 0.49 µs
muParser LabVIEW API (bulk mode): 0.02 µs
Rolf's parser: 0.84 µs
10000 * (parsing + evalution), average time per iteration
LabVIEW Formula Parser: 186.1 µs
muParser LabVIEW API: 158.8 µs
Rolf's Parser: 38.74 µs
05-22-2018 04:30 PM
Hmm your speeds look better than mine, despite having not that different hardware.
Mine is a "Core i7-6600 CPU @ 2.60 GHz 2.81 GHz" according to the System Control panel, but it is a Laptop currently operating on battery, so I suppose that could make a difference as the CPU is very likely not operating at max speed in that case.
05-22-2018 04:57 PM
Perhaps it is due to optimizations in LabVIEW 2015 versus LabVIEW 8?