25.4 Lua Script Performance | LabJack
 
« Close

Datasheets and User Guides

App Notes

Software & Driver

 

25.4 Lua Script Performance

Overview

There are many ways to measure and improve the performance of a Lua Script running on a T-Series device. A set of benchmarking example scripts are available in the online Lua Script Examples and in Kipling.

System Under Test

The performance measurements below were taken using a T4 with FW 1.0023 with default Lua throttling settings and processor speed.  Times will vary slightly between different firmware versions and device type but total execution times (for 100,000) iterations are typically within 100ms.  A T7-Pro with FW 1.0282 was also tested.

Using local to Improve Lookup Time

Define Variables as local

When performing operations in a scripted language like Lua, it takes time to look up variables depending on their scope. To speed up a script, define variables as local. For example, the following script takes 1.72 seconds to execute:

x = 0
for i = 1, 100000 do
    x = x + 1
end

The same script with variable x defined as a local variable takes approximately 850 ms to execute.

local x = 0
for i = 1, 100000 do
  x = x + 1
end

Create local References to Functions

It is also faster to use local references to functions. The benchmarking example scripts all define local references to the functions from the Lua LabJack Library, which provides the global MB and LJ objects. For example, the following script takes 2.37 seconds to execute:

for i = 1, 100000 do
  LJ.DIO_S_W(5,1)
end

The same script with a local reference to the LJ.DIO_S_W as dioStateWrite takes 1.81 seconds to execute.

local dioStateWrite = LJ.DIO_S_W
for i = 1, 100000 do
  dioStateWrite(5,1)
end

Adjusting LJ.setLuaThrottle

After a script is successfully downloaded to a device and no RAM errors are encountered, the T-Series device uses its Lua internal compiler to translate (precompile) the source code into a set of instructions similar to machine code but intended for the Lua interpreter. When it is time for the code to be executed, the T-Series device steps through one or more instructions at a time as defined by the LJ.setLuaThrottle function. In practical terms, this setting adjusts the number of Lua machine instruction codes that are executed per service interval. This number can be increased at the expense of potentially preventing other device features from executing as quickly, such as processing packets from a host computer.

The following code takes approximately 840 ms to execute:

LJ.setLuaThrottle(10)
local x = 1
for i = 1, 100000 do
  x = x + 1
end

The same loop takes approximately 1.08 s to execute when the device is configured to execute 100 instructions per service interval:

LJ.setLuaThrottle(100)
local x = 1
for i = 1, 100000 do
  x = x + 1
end

Benchmark Comparison

There are three ways to toggle a digital I/O line using the LabJack Lua Library functions:

Below, each of the three methods are shown with benchmark results. Note that these benchmark tests use local function references.

MB.writeName - Modbus Name Function

MB.writeName: approximately 24 μs per iteration.

LJ.setLuaThrottle(1000)
local modbus_write_name = MB.writeName
for i = 1, 100000 do
  modbus_write_name("FIO5", 1)
end

MB.W - Modbus Address Function

MB.W: approximately 17 μs per iteration.

LJ.setLuaThrottle(1000)
local mbW = MB.W
for i = 1, 100000 do
  mbW(2005, 0, 1)
end

LJ.DIO_S_W - Shortcut Function

LJ.DIO_S_W: approximately 11 μs per iteration.

LJ.setLuaThrottle(1000)
local dioStateWrite = LJ.DIO_S_W
for i = 1, 100000 do
  dioStateWrite(5,1)
end

Comparison with External Communications

Since T-Series devices implement a Modbus Server that can be controlled directly by Lua or by a host computer, the performance of these functions can be useful to compare to round trip command-response times issued by host computers through the USB, Ethernet, or WiFi communication interfaces.

Further Reading