Python windows 中的脚本不使用测量点之间的时间
Python script in windows uses no time between measure points
我创建了一个使用套接字传输文件 (client/server) 的小程序。在我的发送功能中,我测量了执行某些任务所需的时间,以估计传输的平均速度。
while (l):
start_time = time.time()
self.__sendData(l)
acc += len(l)
self.__setProgress(acc)
l = f.read(1024)
elapsed_time = time.time() - start_time
print("longitud: " + str(len(l)))
print("total: " + str(elapsed_time))
self.__setSpeed(len(l), elapsed_time)
在这里你可以看到我有一个 acc
(umulator) var 来计算传输的总进度。我读取了文件的下一个 1024
字节,并计算了执行这些操作所需的时间。我的函数 __setSpeed
只是使用最新的 50
即时速度计算平均速度。
这个 在 Linux 中工作正常,完全没有问题。但是,当我在 Windows 中 运行 时,我发现了以下输出:
如您所见,很多时候 time for transfer and file read
恰好是 zero
。我不明白为什么会这样,因为:
- 我可以想象(只是猜测)在 Windows 中,文件可能会被缓冲,因此文件的
readtime
变为 0。好的。
sendData
函数可能非常快,因为它的数量非常少。好的,但真的是零吗?
- 另外,我们应该添加一些非常少量的其他命令来执行,比如控制结构,在我的函数中等等。
所以基本上我的问题有两个:
- 为什么会这样?我应该用其他方法测量吗?
- 哪种方法准确且适合在OS、Windows和Linux中使用?
在Windows中,time.time
基于GetSystemTimeAsFileTime
. We can get the resolution of this clock with time.get_clock_info
. For the system clock it calls GetSystemTimeAdjustment
。默认分辨率为 15.625 毫秒:
>>> time.get_clock_info('time').resolution
0.015625
time.perf_counter
should solve your problem. In Windows it's based on QueryPerformanceCounter
. To get the resolution of this clock, get_clock_info
calls QueryPerformanceFrequency
。通常为 100 纳秒:
>>> time.get_clock_info('perf_counter').resolution
1e-07
我创建了一个使用套接字传输文件 (client/server) 的小程序。在我的发送功能中,我测量了执行某些任务所需的时间,以估计传输的平均速度。
while (l):
start_time = time.time()
self.__sendData(l)
acc += len(l)
self.__setProgress(acc)
l = f.read(1024)
elapsed_time = time.time() - start_time
print("longitud: " + str(len(l)))
print("total: " + str(elapsed_time))
self.__setSpeed(len(l), elapsed_time)
在这里你可以看到我有一个 acc
(umulator) var 来计算传输的总进度。我读取了文件的下一个 1024
字节,并计算了执行这些操作所需的时间。我的函数 __setSpeed
只是使用最新的 50
即时速度计算平均速度。
这个 在 Linux 中工作正常,完全没有问题。但是,当我在 Windows 中 运行 时,我发现了以下输出:
如您所见,很多时候 time for transfer and file read
恰好是 zero
。我不明白为什么会这样,因为:
- 我可以想象(只是猜测)在 Windows 中,文件可能会被缓冲,因此文件的
readtime
变为 0。好的。 sendData
函数可能非常快,因为它的数量非常少。好的,但真的是零吗?- 另外,我们应该添加一些非常少量的其他命令来执行,比如控制结构,在我的函数中等等。
所以基本上我的问题有两个:
- 为什么会这样?我应该用其他方法测量吗?
- 哪种方法准确且适合在OS、Windows和Linux中使用?
在Windows中,time.time
基于GetSystemTimeAsFileTime
. We can get the resolution of this clock with time.get_clock_info
. For the system clock it calls GetSystemTimeAdjustment
。默认分辨率为 15.625 毫秒:
>>> time.get_clock_info('time').resolution
0.015625
time.perf_counter
should solve your problem. In Windows it's based on QueryPerformanceCounter
. To get the resolution of this clock, get_clock_info
calls QueryPerformanceFrequency
。通常为 100 纳秒:
>>> time.get_clock_info('perf_counter').resolution
1e-07