私は現在、特定の時点から経過した時間を表示するプログラムを作成しています。あなたが望むなら、ストップウォッチ。時間の不正確または非効率なコード?
私は最終的に私のコードを動作させましたが、判明したように、あまり正確ではありません。それは非常に迅速に遅れています。最初の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()
から来ているかどうか、それとも単純に私の非効率的なコードかどうかはわかりません。これは私がスレッドを使用するのに必要な作業ですか?
私はちょっとした検索を行い、他の人が時間モジュールで他の機能について話しているのを見ましたが、それらのどれも私のためにはうまくいきませんでした。
これは十分な情報ではないか、私は簡単な間違いをしたかどうか教えてください。
'time.clock'は、さまざまなオペレーティングシステム上のさまざまなことを意味します。非常に面倒ですが、そこにはあります。 Linuxの場合、実行時間は経過していますが、壁時計の時間ではなく、秒をカウントするために使用しないでください。 – tdelaney
@tdelaneyそれは意味があります。あなたは良い選択肢を知っていますか?日時モジュールを使用してこの同じ問題にぶつかるのですか、それとも次のステップですか? – Ophi
これを修正するより現代的な方法があるかどうかはわかりませんが、私がLinux(高解像度の壁時計はtime.time())またはWindows(高解像度壁時計はtime.clock)を 'platform.system()'で呼び出し、そこから時計を選択します。 – tdelaney