Why inputsinglescan is much faster than read (data acquisition toolbox)
10 views (last 30 days)
Show older comments
Hello
I noticed that, to read input values from a National Instruments card, "inputsinglescan" is much faster than "read", by almost one order of magnitude.
Why is it the case? Is there a way to obtain a faster read using "read"?
I tested this with the code attached below, on two different PCs with distinct NI cards, and also different Matlab versions (R2020b and R2022a).
In PC1, each run of "inputsinglescan" took on average 0.2 ms, "read" took 1.5 ms.
In PC2, each run of "inputsinglescan" took on average 0.1 ms, "read" took 0.8 ms.
% From Matlab R2020a it is recommeded to use DataAcquisition object,
% instead of the "old" session object functions.
UseOldSessionObjectFunctions = true;
n_samples = 1000;
if UseOldSessionObjectFunctions
daq.reset;
dev_list = daq.getDevices;
device = dev_list(1).ID;
% device = 'Dev1'; % This needs to be specified for your setup
s = daq.createSession('ni');
addDigitalChannel(s, device,'port0/line0','InputOnly');
t = zeros(n_samples,1);
for i = 1 : n_samples
tic
inputSingleScan(s); % first run of inputSingleScan is slower
t(i) = toc;
end
else % Matlab version must be >= R2020a
daqreset;
dev_list = daqlist;
device = dev_list.DeviceID(1); % This needs to be specified for your setup
% device = 'Dev1'; % This needs to be specified for your setup
s = daq('ni');
addinput(s, device, 'port0/line0','Digital');
t = zeros(n_samples,1);
for i = 1 : n_samples
tic
read(s,1,"OutputFormat","Matrix"); % first run of read is slower
% read(s); % first run of read is slower
t(i) = toc;
end
end
t_mean = mean(t(2:end));
figure; plot(t);
title(['Mean dt (excluding first one) = ' num2str(t_mean) ]);
0 Comments
Answers (1)
Shubham
on 29 May 2023
Hi Alessandro,
The difference in the execution time between "inputSingleScan" and "read" functions could be related to various factors, such as the underlying architecture of the National Instruments card, the data size to be read, and the interface between the card and MATLAB.
One possible reason for "inputSingleScan" being faster than "read" could be the way these functions read data. "inputSingleScan" reads a single sample of data from the National Instruments card, while "read" reads a buffer of data. Therefore, "inputSingleScan" could be faster because it reads smaller amounts of data, which the National Instruments card can transmit more quickly.
Another possible reason could be the way MATLAB communicates with the National Instruments card. "inputSingleScan" uses a direct memory access (DMA) technique, which allows data to be transferred directly between the National Instruments card and the computer's memory without involving the CPU. On the other hand, "read" may use a CPU-based approach, which involves more overhead and can be slower.
Regarding the way to obtain a faster read using "read," one option could be to increase the buffer size used by the function. This approach could reduce the overhead associated with multiple calls to "read" to obtain data, thus improving the function's overall performance. However, increasing the buffer size could also result in longer overhead times for data transmission, which could offset the performance gains obtained by reading larger data chunks.
To sum up, the reasons for the difference in execution time between "inputSingleScan" and "read" could be due to various factors related to the National Instruments card and the way MATLAB interfaces with it. Adjusting the buffer size used by "read" could potentially improve its performance, but it is essential to consider the potential downsides of this approach.
0 Comments
See Also
Categories
Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!