Monday 15 March 2010

tkinter - Paradoxical time intervals in Python -



tkinter - Paradoxical time intervals in Python -

i'm new programming (python) learning exercise, wrote tkinter programme measure time between 2 "simultaneous" keypresses. wanted see how much of measured time artifactual actual processing of program, removed actual input , have programme simulate 1 keypress after another. got short interval expected (25-35 microseconds). here's code gave me (just of import part, not whole program):

def buttonpress1(event): = datetime.datetime.now() asec = a.microsecond press1.set(asec) onepressed.set(true) if onepressed.get() == true , twopressed.get() == true: difference() b = datetime.datetime.now() bsec = b.microsecond press2.set(bsec) twopressed.set(true) if onepressed.get() == true , twopressed.get() == true: difference() def difference(): dif = abs(press1.get() - press2.get()) # difference in times. around 30 microseconds resultstr = str(dif) + " microseconds" result.set(resultstr) # result displayed in label widget onepressed.set(false) twopressed.set(false)

then wanted see how much complexity of code adding interval, tried real simple example, strangely, i'm getting longer intervals (around 300 microseconds), finish opposite of expected. here's code:

import datetime = datetime.datetime.now() b = datetime.datetime.now() asec = a.microsecond bsec = b.microsecond print bsec-asec # result around 300 microseconds

can explain me?

datetime.microsecond microsecond component of datetime object - it's not entire datetime represented in microseconds.

in order difference between datetime objects, subtract them , you'll timedelta object:

>>> d1 = datetime.now() >>> d2 = datetime.now() >>> delta = d2 - d1 >>> delta datetime.timedelta(0, 12, 431220) >>> delta.seconds 12 >>> delta.microseconds 431220

so difference here 12.4 seconds, or 12 seconds , 431220 microseconds.

for measuring elapsed time between 2 events however, time.time() easier use, stated @casualdemon in comments:

>>> import time >>> start = time.time() >>> end = time.time() >>> elapsed = end - start >>> elapsed 5.727240085601807 # in seconds

python tkinter

No comments:

Post a Comment