06-25-2020 03:49 AM
Hi,
I want to create a new channel of a specific size. Therefore I use the SetValues() and a for-loop (see attached file: Create_Channel_custom_size.VBS)
However, I wonder how I could pre-initialize the custom length [I think this would be more performant]
Thanks in advanced for support!
Solved! Go to Solution.
06-25-2020 11:33 AM
There are a lot of things to speed the code up.
Option Explicit
Dim ChnName, ChnSz, Inc
ChnName = "My_Channel"
ChnSz = 100000
Inc = 25
Call Data.Root.Clear
' Create Channel
Call Data.Root.ChannelGroups.Add("Group1")
Call Data.Root.ChannelGroups(1).Channels.Add(ChnName,DataTypeChnFloat64)
' Set Values for new Channel
Dim i, value
value = 1
stopwatchReset(1)
for i = 1 to ChnSz
Call Data.Root.ChannelGroups(1).Channels(ChnName).SetValues(value, i)
value = value + 1
if value > Inc Then
value = 1
end if
next
LogFileWrite "Took " & stopwatch(1)
This is your code that uses 4.1 seconds on my machine.
Lets prepare the channel to have the correct size by calling
dim chObj : set chObj = Data.Root.ChannelGroups(1).Channels.Add(ChnName,DataTypeChnFloat64)
chObj.ReservedSize = ChnSz
leading to the following script.
Option Explicit
' Input
Dim ChnName, ChnSz, Inc
ChnName = "My_Channel"
ChnSz = 100000
Inc = 25
Call Data.Root.Clear
' Create Channel
Call Data.Root.ChannelGroups.Add("Group1")
dim chObj : set chObj = Data.Root.ChannelGroups(1).Channels.Add(ChnName,DataTypeChnFloat64)
chObj.ReservedSize = ChnSz
' Set Values for new Channel
Dim i, value
value = 1
stopwatchReset(1)
for i = 1 to ChnSz
Call Data.Root.ChannelGroups(1).Channels(ChnName).SetValues(value, i)
value = value + 1
if value > Inc Then
value = 1
end if
next
LogFileWrite "Took " & stopwatch(1)
This script is consuming 2.2 seconds on my machine.
Now another important change. Lets use the chObj directly in the inner loop.
Data.Root.ChannelGroups(1).Channels(ChnName)
is an expensive lookup. So we just use
chObj.Values(i) = value
leading to the following script.
Option Explicit
' Input
Dim ChnName, ChnSz, Inc
ChnName = "My_Channel"
ChnSz = 100000
Inc = 25
Call Data.Root.Clear
' Create Channel
Call Data.Root.ChannelGroups.Add("Group1")
dim chObj : set chObj = Data.Root.ChannelGroups(1).Channels.Add(ChnName,DataTypeChnFloat64)
chObj.ReservedSize = ChnSz
' Set Values for new Channel
Dim i, value
value = 1
stopwatchReset(1)
for i = 1 to ChnSz
chObj.Values(i) = value
value = value + 1
if value > Inc Then
value = 1
end if
next
LogFileWrite "Took " & stopwatch(1)
finishing in 0.3 seconds on my machine.
This is the most uimportant change. Even removing ReservedSize from the script will only show minor effects if the channel isn't really huge.
Please try to use channel objects whereever possible instead of lookups.
dim chObj : set chObj = Data.Root.ChannelGroups(1).Channels(ChnName)
The last piece of boost can be retrieved by using
chObj(i) = value
which removes another indirection in teh script code.
Equal but different in performance
chObj(i) = value
chObj.values(i) = value
chObj.SetValues(value,i)
06-26-2020 12:39 AM
Hi Andreas,
thank you very much for this detailled performance-analysis. Great insight in this topic!