LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Parse formula in Labview


@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?   

0 Kudos
Message 11 of 22
(1,358 Views)

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).

Message 12 of 22
(1,339 Views)

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 Smiley Very Happy.

 

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.

Message 13 of 22
(1,320 Views)

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. 

Rolf Kalbermatter
Averna BV
Message 14 of 22
(1,309 Views)

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


0 Kudos
Message 15 of 22
(1,289 Views)

Rolf, any chance of posting a copy of your parser for LV 8+?

0 Kudos
Message 16 of 22
(1,285 Views)

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..

Rolf Kalbermatter
Averna BV
Message 17 of 22
(1,265 Views)

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

 

0 Kudos
Message 18 of 22
(1,254 Views)

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.

Rolf Kalbermatter
Averna BV
0 Kudos
Message 19 of 22
(1,243 Views)

Perhaps it is due to optimizations in LabVIEW 2015 versus LabVIEW 8?

0 Kudos
Message 20 of 22
(1,235 Views)