- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何在 Python 中获得毫秒和微秒分辨率的时间戳?
我也想要类似 Arduino 的 delay()
(以毫秒为单位延迟)和 delayMicroseconds()
职能。
在问这个问题之前,我阅读了其他答案,但它们依赖于 time
模块,在 Python 3.3 之前没有任何类型的保证分辨率。它的分辨率无处不在。最受好评的答案 here引用 16 毫秒的 Windows 分辨率(使用他们的答案),即 32000次比我在这里提供的答案更糟糕(0.5 us 分辨率)。再次,我需要 1 毫秒 和 1我们 (或类似)决议,而不是 16000我们解决。
有关的:
最佳答案
对于 Windows:这是一个适用于 Linux(也适用于 Python 3.3 之前的版本)和 Windows 的全功能模块:
函数和代码示例。
功能包括:
"""
GS_timing.py
-create some low-level Arduino-like millis() (milliseconds) and micros()
(microseconds) timing functions for Python
By Gabriel Staples
http://www.ElectricRCAircraftGuy.com
-click "Contact me" at the top of my website to find my email address
Started: 11 July 2016
Updated: 13 Aug 2016
History (newest on top):
20160813 - v0.2.0 created - added Linux compatibility, using ctypes, so that it's compatible with pre-Python 3.3 (for Python 3.3 or later just use the built-in time functions for Linux, shown here: https://docs.python.org/3/library/time.html)
-ex: time.clock_gettime(time.CLOCK_MONOTONIC_RAW)
20160711 - v0.1.0 created - functions work for Windows *only* (via the QPC timer)
References:
WINDOWS:
-personal (C++ code): GS_PCArduino.h
1) Acquiring high-resolution time stamps (Windows)
-https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx
2) QueryPerformanceCounter function (Windows)
-https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
3) QueryPerformanceFrequency function (Windows)
-https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx
4) LARGE_INTEGER union (Windows)
-https://msdn.microsoft.com/en-us/library/windows/desktop/aa383713(v=vs.85).aspx
-*****https://stackoverflow.com/questions/4430227/python-on-win32-how-to-get-
absolute-timing-cpu-cycle-count
LINUX:
-https://stackoverflow.com/questions/1205722/how-do-i-get-monotonic-time-durations-in-python
"""
import ctypes, os
#Constants:
VERSION = '0.2.0'
#-------------------------------------------------------------------
#FUNCTIONS:
#-------------------------------------------------------------------
#OS-specific low-level timing functions:
if (os.name=='nt'): #for Windows:
def micros():
"return a timestamp in microseconds (us)"
tics = ctypes.c_int64()
freq = ctypes.c_int64()
#get ticks on the internal ~2MHz QPC clock
ctypes.windll.Kernel32.QueryPerformanceCounter(ctypes.byref(tics))
#get the actual freq. of the internal ~2MHz QPC clock
ctypes.windll.Kernel32.QueryPerformanceFrequency(ctypes.byref(freq))
t_us = tics.value*1e6/freq.value
return t_us
def millis():
"return a timestamp in milliseconds (ms)"
tics = ctypes.c_int64()
freq = ctypes.c_int64()
#get ticks on the internal ~2MHz QPC clock
ctypes.windll.Kernel32.QueryPerformanceCounter(ctypes.byref(tics))
#get the actual freq. of the internal ~2MHz QPC clock
ctypes.windll.Kernel32.QueryPerformanceFrequency(ctypes.byref(freq))
t_ms = tics.value*1e3/freq.value
return t_ms
elif (os.name=='posix'): #for Linux:
#Constants:
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h> here: https://github.com/torvalds/linux/blob/master/include/uapi/linux/time.h
#prepare ctype timespec structure of {long, long}
class timespec(ctypes.Structure):
_fields_ =\
[
('tv_sec', ctypes.c_long),
('tv_nsec', ctypes.c_long)
]
#Configure Python access to the clock_gettime C library, via ctypes:
#Documentation:
#-ctypes.CDLL: https://docs.python.org/3.2/library/ctypes.html
#-librt.so.1 with clock_gettime: https://docs.oracle.com/cd/E36784_01/html/E36873/librt-3lib.html #-
#-Linux clock_gettime(): http://linux.die.net/man/3/clock_gettime
librt = ctypes.CDLL('librt.so.1', use_errno=True)
clock_gettime = librt.clock_gettime
#specify input arguments and types to the C clock_gettime() function
# (int clock_ID, timespec* t)
clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
def monotonic_time():
"return a timestamp in seconds (sec)"
t = timespec()
#(Note that clock_gettime() returns 0 for success, or -1 for failure, in
# which case errno is set appropriately)
#-see here: http://linux.die.net/man/3/clock_gettime
if clock_gettime(CLOCK_MONOTONIC_RAW , ctypes.pointer(t)) != 0:
#if clock_gettime() returns an error
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return t.tv_sec + t.tv_nsec*1e-9 #sec
def micros():
"return a timestamp in microseconds (us)"
return monotonic_time()*1e6 #us
def millis():
"return a timestamp in milliseconds (ms)"
return monotonic_time()*1e3 #ms
#Other timing functions:
def delay(delay_ms):
"delay for delay_ms milliseconds (ms)"
t_start = millis()
while (millis() - t_start < delay_ms):
pass #do nothing
return
def delayMicroseconds(delay_us):
"delay for delay_us microseconds (us)"
t_start = micros()
while (micros() - t_start < delay_us):
pass #do nothing
return
#-------------------------------------------------------------------
#EXAMPLES:
#-------------------------------------------------------------------
#Only executute this block of code if running this module directly,
#*not* if importing it
#-see here: http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm
if __name__ == "__main__": #if running this module as a stand-alone program
#print loop execution time 100 times, using micros()
tStart = micros() #us
for x in range(0, 100):
tNow = micros() #us
dt = tNow - tStart #us; delta time
tStart = tNow #us; update
print("dt(us) = " + str(dt))
#print loop execution time 100 times, using millis()
print("\n")
tStart = millis() #ms
for x in range(0, 100):
tNow = millis() #ms
dt = tNow - tStart #ms; delta time
tStart = tNow #ms; update
print("dt(ms) = " + str(dt))
#print a counter once per second, for 5 seconds, using delay
print("\nstart")
for i in range(1,6):
delay(1000)
print(i)
#print a counter once per second, for 5 seconds, using delayMicroseconds
print("\nstart")
for i in range(1,6):
delayMicroseconds(1000000)
print(i)
如果您知道如何在 Linux 中获得上述毫秒和微秒分辨率的时间戳,请发帖,因为这也非常有帮助。
time.sleep()
函数不一定是高分辨率函数。 *在我的 Windows 机器上,它的分辨率可能是
8ms 至多 ,而我上面的模块有
0.5us 分辨率 (
10 倍 104567919) 107 倍更好的 10 倍。import time
import GS_timing as timing
def delayMicroseconds(n):
time.sleep(n / 1000000.)
def delayMillisecond(n):
time.sleep(n / 1000.)
t_start = 0
t_end = 0
#using time.sleep
print('using time.sleep')
print('delayMicroseconds(1)')
for x in range(10):
t_start = timing.micros() #us
delayMicroseconds(1)
t_end = timing.micros() #us
print('dt (us) = ' + str(t_end - t_start))
print('delayMicroseconds(2000)')
for x in range(10):
t_start = timing.micros() #us
delayMicroseconds(2000)
t_end = timing.micros() #us
print('dt (us) = ' + str(t_end - t_start))
#using GS_timing
print('\nusing GS_timing')
print('timing.delayMicroseconds(1)')
for x in range(10):
t_start = timing.micros() #us
timing.delayMicroseconds(1)
t_end = timing.micros() #us
print('dt (us) = ' + str(t_end - t_start))
print('timing.delayMicroseconds(2000)')
for x in range(10):
t_start = timing.micros() #us
timing.delayMicroseconds(2000)
t_end = timing.micros() #us
print('dt (us) = ' + str(t_end - t_start))
using time.sleep
delayMicroseconds(1)
dt (us) = 2872.059814453125
dt (us) = 886.3939208984375
dt (us) = 770.4649658203125
dt (us) = 1138.7698974609375
dt (us) = 1426.027099609375
dt (us) = 734.557861328125
dt (us) = 10617.233642578125
dt (us) = 9594.90576171875
dt (us) = 9155.299560546875
dt (us) = 9520.526611328125
delayMicroseconds(2000)
dt (us) = 8799.3056640625
dt (us) = 9609.2685546875
dt (us) = 9679.5439453125
dt (us) = 9248.145263671875
dt (us) = 9389.721923828125
dt (us) = 9637.994262695312
dt (us) = 9616.450073242188
dt (us) = 9592.853881835938
dt (us) = 9465.639892578125
dt (us) = 7650.276611328125
using GS_timing
timing.delayMicroseconds(1)
dt (us) = 53.3477783203125
dt (us) = 36.93310546875
dt (us) = 36.9329833984375
dt (us) = 34.8812255859375
dt (us) = 35.3941650390625
dt (us) = 40.010986328125
dt (us) = 38.4720458984375
dt (us) = 56.425537109375
dt (us) = 35.9072265625
dt (us) = 36.420166015625
timing.delayMicroseconds(2000)
dt (us) = 2039.526611328125
dt (us) = 2046.195068359375
dt (us) = 2033.8841552734375
dt (us) = 2037.4747314453125
dt (us) = 2032.34521484375
dt (us) = 2086.2059326171875
dt (us) = 2035.4229736328125
dt (us) = 2051.32470703125
dt (us) = 2040.03955078125
dt (us) = 2027.215576171875
time
中的低级函数已经在这里访问了分辨率更高的计时器,因为它是一台 Linux 机器(运行 Raspbian)...但是在我的 GS_timing
模块中,我明确调用了 CLOCK_MONOTONIC_RAW 计时器。谁知道否则会使用什么):using time.sleep
delayMicroseconds(1)
dt (us) = 1022.0
dt (us) = 417.0
dt (us) = 407.0
dt (us) = 450.0
dt (us) = 2078.0
dt (us) = 393.0
dt (us) = 1297.0
dt (us) = 878.0
dt (us) = 1135.0
dt (us) = 2896.0
delayMicroseconds(2000)
dt (us) = 2746.0
dt (us) = 2568.0
dt (us) = 2512.0
dt (us) = 2423.0
dt (us) = 2454.0
dt (us) = 2608.0
dt (us) = 2518.0
dt (us) = 2569.0
dt (us) = 2548.0
dt (us) = 2496.0
using GS_timing
timing.delayMicroseconds(1)
dt (us) = 572.0
dt (us) = 673.0
dt (us) = 1084.0
dt (us) = 561.0
dt (us) = 728.0
dt (us) = 576.0
dt (us) = 556.0
dt (us) = 584.0
dt (us) = 576.0
dt (us) = 578.0
timing.delayMicroseconds(2000)
dt (us) = 2741.0
dt (us) = 2466.0
dt (us) = 2522.0
dt (us) = 2810.0
dt (us) = 2589.0
dt (us) = 2681.0
dt (us) = 2546.0
dt (us) = 3090.0
dt (us) = 2600.0
dt (us) = 2400.0
有关的:关于python - 如何在 Python 中获得毫秒和微秒分辨率的时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319606/
我正在使用 JavaFX 开发视频播放器。我正在将视频从本地文件系统加载到 JavaFX 类媒体中。然后我想获取宽度和高度,以便我可以将场景的尺寸设置为该大小: Media media = new M
我是OpenCV的新手,正在尝试解决这个问题。我有一个.mp4文件保存在从网络摄像头流式传输的磁盘上。现在,我需要制作视频的低分辨率版本,以上传到Web服务器(这个想法是,低分辨率预览实时或多或少地上
我在我的应用程序中使用 jfree 图表(折线图)...创建时我的图表看起来很模糊...我怎样才能获得高清晰度图像。 我使用过:ChartUtilities.writeScaledChartAsPNG
我正在用 Java 编写一个工具来处理我网站上上传的文件。我想以特定的方式管理图像:如果尺寸大于限制,我想首先调整图像的尺寸,然后再次检查尺寸。当然,我不想做无用的工作,所以我想在调整图像大小之前估计
是否有一种纯 WPF 方法来获取有关所有可用显示器大小(分辨率)的信息。我知道我可以使用 Screen.AllScreens 来获取它们,但在我的遗留项目中我无法添加新的引用(内置 WPF 程序集除外
看了Developer文档,部分理解了LDPI,HDPI,MDPI,XHDPI类比,网上到处找也没有找到具体答案。 我有两部手机,一部是小屏幕,一部是平板电脑。我希望我的小屏幕手机上的按钮看起来很小,
对于下面给定的网页, xyz {% load staticfiles %} html, body {
我在 Mac 上使用多个 JFrames 制作了一个程序,分辨率还可以,一切都适合屏幕。但是,当我在我的 PC 上运行该程序时,JFrames 太大并且会立即离开屏幕。 我的问题是:是否可以在 JFr
我需要有关如何检索屏幕分辨率的帮助,如下图所示。 我找到了 this documentation这真的很有帮助。这是我根据这些文档尝试的代码: int numberOfScreens = GetSys
有什么方法可以更改分辨率或为亚马逊 Kindle 键盘上的屏幕设置不同的边界?我的屏幕有一部分出现裂纹,我想解决这个问题。 我试过在文件系统中乱搞(通过 WiFi 连接),但我的所有属性更改都没有任何
快速提问, 我正在开发 libgdx 中的游戏,但遇到了问题。我正在尝试扩展我的 Assets ,我正在使用 Gdx.graphics.getDensity() 来获取密度,然后使用它作为乘数来设置我
我有一些关于屏幕分辨率的问题,我不清楚。这些问题假设我的应用程序将在 iOS 4.0 及更高版本以及 iPhone 3GS 或 iPhone 4 上运行。 启动图像 (default.png) 的大小
我们有一个应用程序在审核过程中被 Apple 应用程序审核人员拒绝。拒绝的原因是应用程序在 Ipad 上运行时无法正确缩放。 该应用程序本来就不是要在 Ipad 上运行的,但与他们争论这一点似乎没有意
我试图在不解码文件的情况下获取 JPEG 图像的分辨率。我从互联网上获得了几个样本,但没有一个可以正常工作。似乎是这样,因为许多 JPEG 文件不是标准的,尽管任何图形应用程序(Irfan、PSP、F
我尝试了很多事情,但是所有尝试都失败了。 我需要将灰度图像(2560x1440)调整为更低或更高的分辨率,然后我需要将边界设置为原始大小(2560x1440),但将调整后的图像保持在中心。 我使用的是
我正在使用 GLSulfaceView 开发一个静音相机应用程序,该应用程序从预览中捕获图片。 为了捕捉图片,我使用 GLES20.glReadPixels(0, 0, surfaceWidth, s
是否有相当于 yarn resolutions 的 npm功能? npm package.json docs 中没有提及它. 例如,我想在 3.3.2 上安装 lerna@3.3.2 及其依赖项之一
我制作了一个在 Arduino 上运行的 24x15 LED 显示屏,去年我在内存中放入了 32KB 的图形例程,这很酷。 但现在我已经升级到 Arduino Mega,具有 256KB 内存,并且有
我的场景是,我正在收集网络数据包,如果数据包与网络过滤器匹配,我想记录连续数据包之间的时间差,最后一部分是不起作用的部分。我的问题是,无论我使用什么 C 定时器函数,我都无法获得准确的亚秒级测量。我尝
我们一直在学校布置一项任务,需要更改图像(反转颜色、切换颜色、旋转 90 度、双倍大小、放大图像),现在我正在尝试通过将分辨率加倍来使其大小加倍,但无法弄清楚如何访问该地址并更改它。我们得到了两个函数
我是一名优秀的程序员,十分优秀!