<legend id='2IVDP'><style id='2IVDP'><dir id='2IVDP'><q id='2IVDP'></q></dir></style></legend>

    • <bdo id='2IVDP'></bdo><ul id='2IVDP'></ul>
  1. <i id='2IVDP'><tr id='2IVDP'><dt id='2IVDP'><q id='2IVDP'><span id='2IVDP'><b id='2IVDP'><form id='2IVDP'><ins id='2IVDP'></ins><ul id='2IVDP'></ul><sub id='2IVDP'></sub></form><legend id='2IVDP'></legend><bdo id='2IVDP'><pre id='2IVDP'><center id='2IVDP'></center></pre></bdo></b><th id='2IVDP'></th></span></q></dt></tr></i><div id='2IVDP'><tfoot id='2IVDP'></tfoot><dl id='2IVDP'><fieldset id='2IVDP'></fieldset></dl></div>
  2. <tfoot id='2IVDP'></tfoot>

    <small id='2IVDP'></small><noframes id='2IVDP'>

    1. 如何在 Python 中获得毫秒和微秒分辨率的时间戳

      时间:2023-09-14

      <small id='q2QWC'></small><noframes id='q2QWC'>

      <tfoot id='q2QWC'></tfoot>
    2. <legend id='q2QWC'><style id='q2QWC'><dir id='q2QWC'><q id='q2QWC'></q></dir></style></legend>

            <bdo id='q2QWC'></bdo><ul id='q2QWC'></ul>

              <tbody id='q2QWC'></tbody>

          • <i id='q2QWC'><tr id='q2QWC'><dt id='q2QWC'><q id='q2QWC'><span id='q2QWC'><b id='q2QWC'><form id='q2QWC'><ins id='q2QWC'></ins><ul id='q2QWC'></ul><sub id='q2QWC'></sub></form><legend id='q2QWC'></legend><bdo id='q2QWC'><pre id='q2QWC'><center id='q2QWC'></center></pre></bdo></b><th id='q2QWC'></th></span></q></dt></tr></i><div id='q2QWC'><tfoot id='q2QWC'></tfoot><dl id='q2QWC'><fieldset id='q2QWC'></fieldset></dl></div>

                本文介绍了如何在 Python 中获得毫秒和微秒分辨率的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如何在 Python 中获取毫秒和微秒分辨率的时间戳?
                我还想要类似 Arduino 的 delay() (以毫秒为单位延迟)和 delayMicroseconds() 函数.

                我在问这个问题之前阅读了其他答案,但它们依赖于 time 模块,在 Python 3.3 之前,该模块没有任何类型的保证分辨率.它的分辨率无处不在.here 最受好评的答案引用了 16 毫秒的 Windows 分辨率(使用他们的答案),这比我在此处提供的答案(0.5 us 分辨率)差 32000 倍.同样,我需要 1 ms1 us(或类似)分辨率,不是16000 us分辨率.p>

                相关:

                1. 我自己对如何做的回答C++ 中的相同内容(获取 ms 和 us 分辨率时间戳)

                解决方案

                适用于 Windows: 这是一个适用于 Linux(也适用于 Python 3.3 之前版本)和 Windows 的全功能模块:

                函数和代码示例.
                功能包括:

                • micros()
                • millis()
                • 延迟()
                • delayMicroseconds()

                Python代码模块:

                ""GS_timing.py- 创建一些类似 Arduino 的低级 millis()(毫秒)和 micros()Python 的(微秒)计时函数加布里埃尔·斯台普斯http://www.ElectricRCAAircraftGuy.com- 点击联系我";在我的网站顶部找到我的电子邮件地址开始时间:2016 年 7 月 11 日更新日期:2016 年 8 月 13 日历史(最新):20160813 - 创建 v0.2.0 - 添加了 Linux 兼容性,使用 ctypes,使其与 Python 3.3 之前的版本兼容(对于 Python 3.3 或更高版本,只需使用 Linux 的内置时间函数,如下所示:https://docs.python.org/3/library/time.html)-ex: time.clock_gettime(time.CLOCK_MONOTONIC_RAW)20160711 - 创建了 v0.1.0 - 函数*仅适用于 Windows*(通过 QPC 计时器)参考:视窗:-个人(C++ 代码):GS_PCArduino.h1) 获取高分辨率时间戳 (Windows)-https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx2) QueryPerformanceCounter 函数 (Windows)-https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx3) QueryPerformanceFrequency 函数(Windows)-https://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx4) LARGE_INTEGER 联合 (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-绝对时序 CPU 周期计数LINUX:-https://stackoverflow.com/questions/1205722/how-do-i-get-monotonic-time-durations-in-python""导入 ctypes,操作系统#常量:版本 = '0.2.0'#------------------------------------------------------------------#职能:#------------------------------------------------------------------#OS 特定的低级计时功能:if (os.name=='nt'): #for Windows:定义微():以微秒(us)为单位返回时间戳"抽动 = ctypes.c_int64()频率 = ctypes.c_int64()#获取内部〜2MHz QPC时钟的滴答声ctypes.windll.Kernel32.QueryPerformanceCounter(ctypes.byref(tics))#获取实际频率.内部 ~2MHz QPC 时钟ctypes.windll.Kernel32.QueryPerformanceFrequency(ctypes.byref(freq))t_us = tics.value*1e6/freq.value返回 t_us定义毫秒():以毫秒 (ms) 为单位返回时间戳"抽动 = ctypes.c_int64()频率 = ctypes.c_int64()#获取内部〜2MHz QPC时钟的滴答声ctypes.windll.Kernel32.QueryPerformanceCounter(ctypes.byref(tics))#获取实际频率.内部 ~2MHz QPC 时钟ctypes.windll.Kernel32.QueryPerformanceFrequency(ctypes.byref(freq))t_ms = tics.value*1e3/freq.value返回 t_mselif (os.name=='posix'): #for Linux:#常量:CLOCK_MONOTONIC_RAW = 4 # 见 <linux/time.h>这里:https://github.com/torvalds/linux/blob/master/include/uapi/linux/time.h#prepare {long, long} 的 ctype timespec 结构类时间规范(ctypes.Structure):_字段_ =[('tv_sec', ctypes.c_long),('tv_nsec', ctypes.c_long)]#配置Python访问clock_gettime C库,通过ctypes:#文档:#-ctypes.CDLL:https://docs.python.org/3.2/library/ctypes.html#-librt.so.1 与clock_gettime:https://docs.oracle.com/cd/E36784_01/html/E36873/librt-3lib.html #-#-Linux clock_gettime(): http://linux.die.net/man/3/clock_gettimelibrt = ctypes.CDLL('librt.so.1', use_errno=True)clock_gettime = librt.clock_gettime#指定C clock_gettime() 函数的输入参数和类型# (int clock_ID, timespec* t)clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]def 单调时间():以秒 (sec) 为单位返回时间戳"t = 时间规格()#(请注意,clock_gettime() 返回 0 表示成功,或 -1 表示失败,在# 哪种情况 errno 设置得当)#-见这里:http://linux.die.net/man/3/clock_gettime如果clock_gettime(CLOCK_MONOTONIC_RAW,ctypes.pointer(t))!= 0:#if clock_gettime() 返回错误errno_ = ctypes.get_errno()引发 OSError(errno_, os.strerror(errno_))返回 t.tv_sec + t.tv_nsec*1e-9 #sec定义微():以微秒(us)为单位返回时间戳"返回 monotonic_time()*1e6 #us定义毫秒():以毫秒 (ms) 为单位返回时间戳"返回 monotonic_time()*1e3 #ms#其他计时功能:定义延迟(延迟毫秒):delay_ms 毫秒 (ms) 的延迟"t_start = 毫秒()而 (millis() - t_start < delay_ms):通过#什么都不做返回def delayMicroseconds(delay_us):延迟延迟_us 微秒 (us)"t_start = micros()而 (micros() - t_start < delay_us):通过#什么都不做返回#------------------------------------------------------------------#例子:#------------------------------------------------------------------#如果直接运行这个模块,只执行这个代码块,#*not* 如果导入它#-见这里:http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htmif __name__ == "__main__": #if 将此模块作为独立程序运行#print循环执行时间100次,使用micros()tStart = micros() #us对于范围内的 x (0, 100):tNow = micros() #usdt = tNow - tStart #us;增量时间tStart = tNow #us;更新打印(dt(我们)="+ str(dt))#print循环执行时间100次,使用millis()打印(
                ")tStart = millis() #ms对于范围内的 x (0, 100):tNow = millis() #msdt = tNow - tStart #ms;增量时间tStart = tNow #ms;更新打印(dt(毫秒)="+ str(dt))# 每秒打印一次计数器,持续 5 秒,使用延迟打印(
                开始")对于范围内的 i (1,6):延迟(1000)打印(一)#使用 delayMicroseconds 每秒打印一次计数器,持续 5 秒打印(
                开始")对于范围内的 i (1,6):延迟微秒(1000000)打印(一)

                如果您知道如何在 Linux 中获取上述毫秒和微秒分辨率的时间戳,请发帖,因为这也会很有帮助.

                这也适用于 Linux,包括 Python 3.3 之前的版本,因为我通过 ctypes 模块使用 C 函数来读取时间戳.

                (注意:上面的代码最初发布在这里:http://www.electricrcaircraftguy.com/2016/07/arduino-like-millisecond-and-microsecond-timestamps-in-python.html)

                特别感谢 @ArminRonacher 在此处提供的出色的 Python 3.3 Linux 前答案:https://stackoverflow.com/a/1205762/4561887


                更新:在 Python 3.3 之前,内置 Python 时间库(https:///docs.python.org/3.5/library/time.html) 没有任何明确的高分辨率函数.不过,现在它确实提供了其他选项,包括一些高分辨率功能.

                然而,我上面的模块为 Python 3.3 之前和之后的 Python 代码提供了高分辨率时间戳,并且它在 Linux 和 Windows 上都这样做.

                这是我的意思的一个例子,表明 time.sleep() 函数不一定是高分辨率函数.*在我的 Windows 机器上,它的分辨率可能是 8ms 最好,而我上面的模块有 0.5us 分辨率(16000 倍!) 在同一台机器上.

                代码演示:

                导入时间导入 GS_timing 作为时间def 延迟微秒(n):time.sleep(n/1000000.)定义延迟毫秒(n):time.sleep(n/1000.)t_start = 0t_end = 0#使用时间.sleepprint('使用时间.sleep')打印('延迟微秒(1)')对于范围内的 x (10):t_start = timing.micros() #us延迟微秒(1)t_end = timing.micros() #usprint('dt (us) = ' + str(t_end - t_start))打印('延迟微秒(2000)')对于范围内的 x (10):t_start = timing.micros() #us延迟微秒(2000)t_end = timing.micros() #usprint('dt (us) = ' + str(t_end - t_start))#使用 GS_timingprint('
                使用 GS_timing')print('timing.delayMicroseconds(1)')对于范围内的 x (10):t_start = timing.micros() #us计时延迟微秒(1)t_end = timing.micros() #usprint('dt (us) = ' + str(t_end - t_start))print('timing.delayMicroseconds(2000)')对于范围内的 x (10):t_start = timing.micros() #us计时延迟微秒(2000)t_end = timing.micros() #usprint('dt (us) = ' + str(t_end - t_start))


                我的 WINDOWS 8.1 机器上的示例结果(注意 time.sleep 的效果差多少):

                使用 time.sleep延迟微秒(1)dt (我们) = 2872.059814453125dt (我们) = 886.3939208984375dt (我们) = 770.4649658203125dt (我们) = 1138.7698974609375dt (我们) = 1426.027099609375dt (我们) = 734.557861328125dt (我们) = 10617.233642578125dt (我们) = 9594.90576171875dt (我们) = 9155.299560546875dt (我们) = 9520.526611328125延迟微秒(2000)dt (我们) = 8799.3056640625dt (我们) = 9609.2685546875dt (我们) = 9679.5439453125dt (我们) = 9248.145263671875dt (我们) = 9389.721923828125dt (我们) = 9637.994262695312dt (我们) = 9616.450073242188dt (我们) = 9592.853881835938dt (我们) = 9465.639892578125dt (我们) = 7650.276611328125使用 GS_timing计时延迟微秒(1)dt (我们) = 53.3477783203125dt (我们) = 36.93310546875dt (我们) = 36.9329833984375dt (我们) = 34.8812255859375dt (我们) = 35.3941650390625dt (我们) = 40.010986328125dt (我们) = 38.4720458984375dt (我们) = 56.425537109375dt (我们) = 35.9072265625dt (我们) = 36.420166015625计时延迟微秒(2000)dt (我们) = 2039.526611328125dt (我们) = 2046.195068359375dt (我们) = 2033.8841552734375dt (我们) = 2037.4747314453125dt (我们) = 2032.34521484375dt (我们) = 2086.2059326171875dt (我们) = 2035.4229736328125dt (我们) = 2051.32470703125dt (我们) = 2040.03955078125dt (我们) = 2027.215576171875


                我的 RASPBERRY PI 版本 1 B+ 上的示例结果(请注意,使用 time.sleep 和我的模块之间的结果基本相同......显然 time 中的低级函数已经可以更好地访问- 这里的分辨率计时器,因为它是一台 Linux 机器(运行 Raspbian)...但是在我的 GS_timing 模块中,我明确调用了 CLOCK_MONOTONIC_RAW 计时器.谁知道否则会使用什么):

                使用 time.sleep延迟微秒(1)dt (我们) = 1022.0dt (我们) = 417.0dt (我们) = 407.0dt (我们) = 450.0dt (我们) = 2078.0dt (我们) = 393.0dt (我们) = 1297.0dt (我们) = 878.0dt (我们) = 1135.0dt (我们) = 2896.0延迟微秒(2000)dt (我们) = 2746.0dt (我们) = 2568.0dt (我们) = 2512.0dt (我们) = 2423.0dt (我们) = 2454.0dt (我们) = 2608.0dt (我们) = 2518.0dt (我们) = 2569.0dt (我们) = 2548.0dt (我们) = 2496.0使用 GS_timing计时延迟微秒(1)dt (我们) = 572.0dt (我们) = 673.0dt (我们) = 1084.0dt (我们) = 561.0dt (我们) = 728.0dt (我们) = 576.0dt (我们) = 556.0dt (我们) = 584.0dt (我们) = 576.0dt (我们) = 578.0计时延迟微秒(2000)dt (我们) = 2741.0dt (我们) = 2466.0dt (我们) = 2522.0dt (我们) = 2810.0dt (我们) = 2589.0dt (我们) = 2681.0dt (我们) = 2546.0dt (我们) = 3090.0dt (我们) = 2600.0dt (我们) = 2400.0

                相关:

                1. 我的 3 组时间戳函数(相互交叉):

                  1. 对于 C 时间戳,请在此处查看我的答案:以微秒为单位获取 C 中的时间戳?
                  2. 对于 C++ 高分辨率时间戳,请在此处查看我的答案:在 C++ 中获取准确的执行时间(微秒)
                  3. 对于 Python 高分辨率时间戳,请在此处查看我的答案:如何在 Python 中获取毫秒和微秒分辨率的时间戳?

                How do I get millisecond and microsecond-resolution timestamps in Python?
                I'd also like the Arduino-like delay() (which delays in milliseconds) and delayMicroseconds() functions.

                I read other answers before asking this question, but they rely on the time module, which prior to Python 3.3 did NOT have any type of guaranteed resolution whatsoever. Its resolution is all over the place. The most upvoted answer here quotes a Windows resolution (using their answer) of 16 ms, which is 32000 times worse than my answer provided here (0.5 us resolution). Again, I needed 1 ms and 1 us (or similar) resolutions, not 16000 us resolution.

                Related:

                1. my own answer on how to do the same thing (get ms and us-resolution timestamps) in C++

                解决方案

                For Windows: Here's a fully-functional module for both Linux (works with pre-Python 3.3 too) and Windows:

                Functions and code samples.
                Functions include:

                • micros()
                • millis()
                • delay()
                • delayMicroseconds()

                Python code module:

                """
                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("
                ")
                    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("
                start")
                    for i in range(1,6):
                        delay(1000)
                        print(i)
                
                    #print a counter once per second, for 5 seconds, using delayMicroseconds
                    print("
                start")
                    for i in range(1,6):
                        delayMicroseconds(1000000)
                        print(i)
                

                If you know how to get the above millisecond and microsecond-resolution timestamps in Linux, please post, as that would be very helpful too.

                This works for Linux too, including in pre-Python 3.3, since I'm using C functions via the ctypes module in order to read the time stamps.

                (Note: code above originally posted here: http://www.electricrcaircraftguy.com/2016/07/arduino-like-millisecond-and-microsecond-timestamps-in-python.html)

                Special thanks to @ArminRonacher for his brilliant pre-Python 3.3 Linux answer here: https://stackoverflow.com/a/1205762/4561887


                Update: prior to Python 3.3, the built-in Python time library (https://docs.python.org/3.5/library/time.html) didn't have any explicitly high-resolution functions. Now, however it does provide other options, including some high-resolution functions.

                My module above, however, provides high-resolution timestamps for Python code before Python 3.3, as well as after, and it does so on both Linux and Windows.

                Here's an example of what I mean, showing that the time.sleep() function is NOT necessarily a high-resolution function. *On my Windows machine, it's resolution is perhaps 8ms at best, whereas my module above has 0.5us resolution (16000 times better!) on the same machine.

                Code demonstration:

                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('
                using 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))
                


                SAMPLE RESULTS ON MY WINDOWS 8.1 MACHINE (notice how much worse time.sleep does):

                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
                


                SAMPLE RESULTS ON MY RASPBERRY PI VERSION 1 B+ (notice that the results between using time.sleep and my module are basically identical...apparently the low-level functions in time are already accessing better-resolution timers here, since it's a Linux machine (running Raspbian)...BUT in my GS_timing module I am explicitly calling the CLOCK_MONOTONIC_RAW timer. Who knows what's being used otherwise):

                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
                

                Related:

                1. My 3 sets of timestamp functions (cross-linked to each other):

                  1. For C timestamps, see my answer here: Get a timestamp in C in microseconds?
                  2. For C++ high-resolution timestamps, see my answer here: Getting an accurate execution time in C++ (micro seconds)
                  3. For Python high-resolution timestamps, see my answer here: How can I get millisecond and microsecond-resolution timestamps in Python?

                这篇关于如何在 Python 中获得毫秒和微秒分辨率的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将 datetime 转换为 Unix 时间戳并将其转换回 pytho 下一篇:时间戳中的 T 和 Z 到底是什么意思?

                相关文章

                最新文章

                <tfoot id='ycG9o'></tfoot>
              • <i id='ycG9o'><tr id='ycG9o'><dt id='ycG9o'><q id='ycG9o'><span id='ycG9o'><b id='ycG9o'><form id='ycG9o'><ins id='ycG9o'></ins><ul id='ycG9o'></ul><sub id='ycG9o'></sub></form><legend id='ycG9o'></legend><bdo id='ycG9o'><pre id='ycG9o'><center id='ycG9o'></center></pre></bdo></b><th id='ycG9o'></th></span></q></dt></tr></i><div id='ycG9o'><tfoot id='ycG9o'></tfoot><dl id='ycG9o'><fieldset id='ycG9o'></fieldset></dl></div>
              • <legend id='ycG9o'><style id='ycG9o'><dir id='ycG9o'><q id='ycG9o'></q></dir></style></legend>

                <small id='ycG9o'></small><noframes id='ycG9o'>

                      <bdo id='ycG9o'></bdo><ul id='ycG9o'></ul>