DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Performance of Calculate and ANALYSIS Basic mathematics functions

Hello DIAdem enthusiasts,

A colleague who optimized one of our scripts with the help of the Script-Profiler discovered that the countless Calculate commands generate about 97% of the computing time.

 

A large part of these commands are simple mathematical operations, which could also be covered by DIAdem's built-in channel calculation commands (ANALYSIS basic mathematics). Unfortunately, this becomes much more confusing and is not very natively usable/readable.

Call Data.Root.Clear()
Call LogFileDel()

Dim oRoot : Set oRoot = Data.Root
Dim oGrp : Set oGrp = oRoot.ChannelGroups.Add("Test")
Dim oCalcChn : Set oCalcChn = oGrp.Channels.Add("CalcChn", DataTypeChnFloat64)
Dim iCount

R1 = 1
R2 = 0
Call ChnLinGen(oCalcChn,1,500000,500000)

Call StopWatchReset(1)
For iCount = 1 To 8
  Call Calculate("Ch(""[1]/CalcChn"") = (Ch(""[1]/CalcChn"")+R2)/R1")
Next
Call LogFileWrite("calculate and real variables: " & Str(Stopwatch(1),"d.ddd") & " s")

Call StopWatchReset(1)
For iCount = 1 To 8
  Call Calculate("Ch(""[1]/CalcChn"") = (Ch(""[1]/CalcChn"")+0)/1")
Next
Call LogFileWrite("calculate and hard coded numbers: " & Str(Stopwatch(1),"d.ddd") & " s")

Call StopWatchReset(1)
For iCount = 1 To 8
  Call ChnLinGen("R1", R1, R1, oCalcChn.Size)
  Call ChnOffset(oCalcChn, oCalcChn, R2, "free offset")
  Call ChnDiv(oCalcChn, oGrp.Channels("R1"), oCalcChn)
  Call oGrp.Channels.Remove("R1")
Next
Call LogFileWrite("analysis commands: " & Str(Stopwatch(1),"d.ddd") & " s")

Set oRoot = Nothing : Set oGrp = Nothing : Set oCalcChn = Nothing

In this example the computing time on my computer differs by factor 41. Any idea how to speed up the Calculate command?!

 

Cheers,

Thomas

0 Kudos
Message 1 of 7
(3,795 Views)

I have exactly the same issue. Using simple channel operations like ChnAdd, ChnMul, ChnSub etc. can help to speed up the script significantly, but they make your script look like a mess and at some point you need ChnCalculate anyway if formulas get more complex. Anyone here who can shed some light on why ChnCalculate is so annoyingly slow?

0 Kudos
Message 2 of 7
(3,641 Views)

When you define an equation with the CALCULATE command, a complex tree is created in the background to be able to calculate your mathematical function. This is very flexible but can take a bit time in case you have very long channels.

The fastest way to calculate is to use the functions with a fixed mathematical content like ChnDiv, ChnSum, etc. This means to split the equation in parts for which you can use these functions.

 

Greetings

Walter

0 Kudos
Message 3 of 7
(3,629 Views)

Hi All,

 

Another way to put this is that the formula you submit to the Calculate() function does not execute compiled, while the pre-built functions in the ANALYSIS panel always execute compiled.  As Walter said, if you're concerned about execution speed, your best bet is to only use the Calculate() function where there are no available ANALYSIS commands, such as "ChA=abs(ChA)" or "ChA=sin(ChB)".

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 4 of 7
(3,617 Views)

Thanks Walter and Brad for your comments. To use the pre-built functions is actually the workaround that I am already using to speed up my scripts. But the disadvantage is, as described earlier, that it blows up the script and makes the equations almost unreadable.

I wonder if it would be possible for a future release to incorporate an analyzer that can detect if a Calculate command could be replaced by pre-built functions? If yes, then could it be realized that DIAdem automatically splits the Calculate command into the pre-built functions?

Message 5 of 7
(3,586 Views)

Hi Phex,

 

That strikes me as an excellent suggestion for the DIAdemIdeas exchange.  It'll mean more coming directly from a customer...

 

www.ni.com/diademideas

 

Brad Turpin

Senior Technical Support Engineer

National Instruments

0 Kudos
Message 6 of 7
(3,501 Views)

DIAdem Idea Exchange, Calculate analyzer for basic mathemetical functions: https://forums.ni.com/t5/DIAdem-Idea-Exchange/Calculate-analyzer-for-basic-mathemetical-functions/id...

0 Kudos
Message 7 of 7
(3,458 Views)