Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

Why the output rate of python code is soo low?

Solved!
Go to solution

Good afternoon I am using the acquistion board PCle-6346 controlled using python to acquire samples from a position sensor which is the Keyenche IL-030S.
Now, i set the sampling frequency of the board at 500 kHz and the buffer size to the minimum of 2 but the python script i made gives out a reading every 0.095 s which is a little more than 10 Hz. As I am quite new in the field I was wondering how i can imoprove the output rate of my code to be at least 100Hz or more.
Thanks to everyone for your time and support 

import numpy as np
import nidaqmx
from nidaqmx.stream_readers import AnalogMultiChannelReader
from nidaqmx.constants import Edge, AcquisitionType, TerminalConfiguration
from nidaqmx.stream_readers import AnalogMultiChannelReader
import time

def acquire_data(buffer_size, freq_sampling😞
    with nidaqmx.Task() as task:
        start_time = time.time()
        task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
        print("time:", time.time() - start_time)
        task.timing.cfg_samp_clk_timing(freq_sampling, sample_mode=AcquisitionType.FINITE, samps_per_chan=buffer_size)
        print("time:", time.time() - start_time)
        data = np.zeros((1, buffer_size))  # Placeholder for the acquired data
        reader = AnalogMultiChannelReader(task.in_stream)
        print("time:", time.time() - start_time)
        reader.read_many_sample(data, number_of_samples_per_channel=buffer_size)
        print("time:", time.time() - start_time, 1/(time.time() - start_time))
        print("\n")
    return data.flatten()  

while True:
    data = acquire_data(2, 500000)
0 Kudos
Message 1 of 3
(127 Views)
Solution
Accepted by topic author filippo1699

Because Python is an interpreter language and is much slower than C or C#. Reference: C++ vs. Python: A Performance Comparison using an Example

Use LabVIEW, C or C# if you want to read the data more frequently. See Getting Started with NI-DAQmx for Text-Based Programming

-------------------------------------------------------
Control Lead | Intelline Inc
Message 2 of 3
(108 Views)

Crystal clear!!
Thank you so much for your fast and precious answer!!!!

0 Kudos
Message 3 of 3
(80 Views)