0

我正在尝试计算传感器读取需要多长时间,但无法让它的时间部分工作,我做错了什么?

import threading
import time

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

1 回答 1

3

您正在重新分配program_time给各种变量。它们不会自动更新它们的值,您需要time.time()再次调用。

>>> 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

您真的想将至少一个调用移到time.time()循环之外:

start = time.time()
while True:
    now = time.time()
    elapsed = now - start
    print elapsed
于 2013-02-16T14:41:50.503 回答