Nick1980
Nick1980

Reputation: 17

Subtracting time in milliseconds

I am trying to time how long it is taking for a sensor to read, but can't get the time part of it to work, what am I doing wrong?

import threading
import time

while True:
    program_time = time.time();
    a = program_time
    b = program_time
    c = program_time
    time = c-a
    print time

Upvotes: 0

Views: 559

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121476

You are reassigning program_time to various variables. They will not automatically update their value, you need to call time.time() again.

>>> import time
>>> time.time()
1361025728.405679
>>> a = time.time()
>>> b = a
>>> b
1361025731.55744
>>> a
1361025731.55744
>>> b - a
0.0
>>> time.time() - b
17.488538026809692

You really want to move at least one call to time.time() outside of your loop:

start = time.time()
while True:
    now = time.time()
    elapsed = now - start
    print elapsed

Upvotes: 3

Related Questions