Python 为什么时间差。性能计数器会随时间变慢

Python 为什么时间差。性能计数器会随时间变慢,python,python-3.x,Python,Python 3.x,我正在做一个学校项目,必须从一个加速计上读取一段时间内插入arduino的值,然后将其写入csv文件。 为了提高读取的准确性,我将捕获者传输的所有值放在一个列表中,每[X]秒平均一次,然后在将这些值写入我的csv文件之前将其存储到最终列表中。 为了做到这一点,我使用time.perf_counter()来精确地知道何时收集这些值 当在1s内对1000个值进行测试时,第一个值很好,只是有一点漂移,但这不会让我太烦恼。但是,在40值之后,读数的间隔为0.01s,而不是0.001s(预期值) 不确定它

我正在做一个学校项目,必须从一个加速计上读取一段时间内插入arduino的值,然后将其写入csv文件。 为了提高读取的准确性,我将捕获者传输的所有值放在一个列表中,每[X]秒平均一次,然后在将这些值写入我的csv文件之前将其存储到最终列表中。 为了做到这一点,我使用time.perf_counter()来精确地知道何时收集这些值

当在1s内对1000个值进行测试时,第一个值很好,只是有一点漂移,但这不会让我太烦恼。但是,在40值之后,读数的间隔为0.01s,而不是0.001s(预期值)

不确定它是否有任何价值,但我正在Pycharm上运行python 3.7

import serial
import time
import csv
import numpy as np
ser = serial.Serial('COM5',9600)
while True:
    print("Initialisation", end="")
    time.sleep(0.2)
    for x in range(0,3):
        print(".", end="")
        time.sleep(0.3)
    entre=input('\nPress S to begin input or Q to quit')
    if entre=='S':
        del entre                                         #On supprime entre pour boucle infinie
        X=[]
        Y=[]
        Z=[]
        total=[]

        temps_de_mesure=1
        nombre_de_mesure = 1000
        intervalle_entre_2_mesures = temps_de_mesure/nombre_de_mesure    #Recupèrer toutes les valeurs
        t0 = time.perf_counter()
        for x in range(0, nombre_de_mesure):
            t_repeat = time.perf_counter()
            liste_temporaire = [[], [], []]

            while time.perf_counter()-t_repeat < 0.001:
                temp = str(ser.readline())
                temp = temp[2:-5]
                temp = temp.split(';')
                liste_temporaire[0].append(int(temp[0]))
                liste_temporaire[1].append(int(temp[1]))
                liste_temporaire[2].append(int(temp[2]))

            t_moyenne=time.perf_counter()-t0
            x_moyenne=np.mean(liste_temporaire[0])
            y_moyenne=np.mean(liste_temporaire[1])
            z_moyenne=np.mean(liste_temporaire[2])

            total.append([t_moyenne,x_moyenne,y_moyenne,z_moyenne])


        print(total) #affichage résultat

        with open('acquision.csv','w', newline='') as csvFile:         #Création d'un fichier csv acquisition avec les valeurs
            writer=csv.writer(csvFile)
            writer.writerows(total)
        csvFile.close()

    elif entre=='Q':break

ser.close()
导入序列号
导入时间
导入csv
将numpy作为np导入
ser=串行。串行('COM5',9600)
尽管如此:
打印(“初始化”,end=“”)
睡眠时间(0.2)
对于范围(0,3)内的x:
打印(“.”,end=“”)
睡眠时间(0.3)
entre=input(“\n按S开始输入,或按Q退出”)
如果entre=='S':
德尔恩特雷(del entre#On supprime entre pour boucle infinie)
X=[]
Y=[]
Z=[]
总计=[]
测量温度=1
标称测量值=1000
测量时间间隔=测量时间/测量名称
t0=时间。性能计数器()
对于范围内的x(0,标称测量):
t_repeat=time.perf_计数器()
临时列表=[]、[]、[]]
当time.perf_counter()-t_repeat<0.001时:
temp=str(ser.readline())
温度=温度[2:-5]
临时=临时拆分(“;”)
liste_temporaire[0]。追加(int(temp[0]))
liste_temporaire[1]。追加(int(temp[1]))
liste_temporaire[2]。追加(int(temp[2]))
t_moyenne=时间。性能计数器()-t0
x_moyenne=np.平均值(列表时间[0])
y_moyenne=np.平均值(列出时间[1])
z_moyenne=np.平均值(列出时间[2])
总计。追加([t_moyenne,x_moyenne,y_moyenne,z_moyenne])
打印(总计)#附加结果
以open('acquision.csv','w',newline='')作为csvFile:#Création d'un fichier csv acquisition avec les valeurs
writer=csv.writer(csvFile)
writer.writerows(总计)
csvFile.close()
elif entre=='Q':中断
塞尔克洛斯()