From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Create new Channel of custom size

Solved!
Go to solution

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!

 

 

 

0 Kudos
Message 1 of 3
(1,876 Views)
Solution
Accepted by topic author MaT_305

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)

 

Message 2 of 3
(1,846 Views)

 

Hi Andreas,

thank you very much for this detailled performance-analysis. Great insight in this topic!

0 Kudos
Message 3 of 3
(1,816 Views)