Python 时间不准确还是代码效率低下?

Python 时间不准确还是代码效率低下?,python,datetime,time,stopwatch,Python,Datetime,Time,Stopwatch,我目前正在开发一个程序,该程序将显示自特定时间点以来经过的时间量。一块秒表,如果你愿意的话 我终于让我的代码正常工作了,但事实证明,它不是很准确。它很快就落后了,在跑步的前10秒内就落后了1秒甚至2秒,我不完全确定这是为什么 # Draw def draw(): stdscr.erase() stdscr.border() # Debugging if debug: stdscr.addstr(5 , 3, "running : %s

我目前正在开发一个程序,该程序将显示自特定时间点以来经过的时间量。一块秒表,如果你愿意的话

我终于让我的代码正常工作了,但事实证明,它不是很准确。它很快就落后了,在跑步的前10秒内就落后了1秒甚至2秒,我不完全确定这是为什么

#   Draw
def draw():
    stdscr.erase()
    stdscr.border()

    #   Debugging
    if debug:
        stdscr.addstr(5 , 3, "running  : %s " % running )
        stdscr.addstr(6 , 3, "new      : %s " % new )
        stdscr.addstr(7 , 3, "pureNew  : %s " % pureNew )
        stdscr.addstr(8 , 3, "paused   : %s " % paused )
        stdscr.addstr(9 , 3, "complete : %s " % complete )
        stdscr.addstr(10, 3, "debug    : %s " % debug )

    if running:
        stdscr.addstr(1, 1, ">", curses.color_pair(8))
        stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.gmtime( timeElapsedTotal ) ) )

    elif not running:
        if new and pureNew:
            stdscr.addstr(1, 1, ">", curses.color_pair(5))
            stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", timeNone ) )
        elif paused:
            stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", t.gmtime( timeElapsedTotal ) ), curses.color_pair(1) )
            stdscr.addstr(1, 1, ">", curses.color_pair(3))
        else:
            stdscr.addstr(1, 1, ">", curses.color_pair(5))
            stdscr.addstr(1, 3, t.strftime( "%H:%M.%S", timeNone ) )

    stdscr.redrawwin()
    stdscr.refresh()
    return

    #   Calculations
def calc():
    global timeElapsedTotal
    if running:
        timeElapsedTotal = t.clock() - timeStart
    return

    #   Main Loop
while True:
    #   Get input from the user
    kInput = stdscr.getch()

    #   If q is pressed we close the program
    if kInput == ord('q'):
        endProg()

    #   If d is pressed we toggle 'debug' mode
    elif kInput == ord('d'):
        debug = not debug

    #   If s is pressed we stop the current run
    elif kInput == ord('s'):
        running = False
        new = True

    #   If spacebar is pressed and we are ready for a new run,
    #       we start a new run
    elif kInput == ord(' ') and new:
        running = not running
        new = not new
        pureNew = False
        timeStart = t.clock()

    #   If p is pressed and we are in the middle of a run,
    #       we pause the run
    elif kInput == ord('p') and not new:
        running = not running
        paused = not paused
        timeStart = t.clock() - timeStart

    calc()
    draw()
据我所知,上述代码按预期工作。我不确定延迟是来自
time.clock()
还是我的代码效率低下。这是我需要使用线程的工作吗

我在谷歌上搜索了一下,看到其他人在谈论时间模块中的其他功能,但这些功能对我来说都没有更好的效果


如果这些信息不够,或者我犯了一个简单的错误,请告诉我。

事实证明,解决方案非常简单,就像tdelaney建议的那样,从
time.clock()
更改为
time.time()


看起来我需要在使用模块时更彻底地阅读它们。感谢您的智慧。

时间。时钟在不同的操作系统上有不同的含义-非常烦人,但它确实存在。在linux上,它经过的执行时间,而不是挂钟时间,不应该用来计算秒数。@tdelaney Ok,这很有意义。你知道有什么好的选择吗?我是否会在使用datetime模块时遇到同样的问题,或者这是下一步?我不知道是否有更现代的方法来解决这个问题,但回到过去,我会通过
platform.system()检查我是在linux(高分辨率wall clock是time.time())还是在windows(高分辨率wall clock是time.clock)上运行
然后从那里选择一个时钟。如果更改时钟有效,请告诉我。@t德莱尼工作得很好。谢谢,我很感激。