Time Inaccuracy or Inefficient Code?
<p>I'm currently working on a program that will display the amount of time that has passed since a specific point. A stopwatch, if you will.</p>

<p>I've finally gotten my code working, but as it turns out, it's not very accurate. It falls behind very quickly, being 1 or even 2 seconds off within just the first 10 seconds of running, I'm not completely sure why this is.</p>

<pre><code>#   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, "&gt;", 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, "&gt;", 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, "&gt;", curses.color_pair(3))
        else:
            stdscr.addstr(1, 1, "&gt;", 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()
</code></pre>

<p>The above code is, as far as I am aware, working as intended. I'm not sure if the lag is coming from <code>time.clock()</code> or if it's simply my inefficient code. Is this the kind of work that I would need to use threads for?</p>

<p>I did a bit of googling and saw others talking about other functions in the time module but none of those worked any better for me.</p>

<p>Let me know if this isn't enough information or I made a simple mistake.</p>

<p>Well as it turns out the solution was as simple as changing from <code>time.clock()</code> to <code>time.time()</code> as suggested by <em>tdelaney</em>. </p>

<p>Looks like I need to more thoroughly read up on modules as I use them. Thanks for the wisdom.</p>

python datetime time stopwatch