简体   繁体   English

脚本中的时间变量在执行期间变为负数

[英]Time variable in script became negative during execution

I have a Python program running on my Raspberry Pi 3B doing a bunch of image processing and so on.我有一个 Python 程序在我的 Raspberry Pi 3B 上运行,执行大量图像处理等。 I wanted to gather some data measurements from the program by writing it into a .csv file, and I wanted to write the corresponding time with each measurement.我想通过将程序写入 .csv 文件来从程序中收集一些数据测量值,并且我想为每个测量值写入相应的时间。 I used time.clock() (see code snippet below) to find the time before each write operation, but somewhere between 2147 seconds and 2148 seconds, the time becomes negative (see snippet table below).我使用time.clock() (请参阅下面的代码片段)来查找每次写入操作之前的时间,但在 2147 秒和 2148 秒之间,时间变为负数(参见下面的片段表)。 I expect some kind over overflow occurred, but I'm having trouble understanding in which manner it overflowed.我预计会发生某种溢出,但我无法理解它以何种方式溢出。 The Raspberry Pi is a 32 bit system, and as I understand, the time.clock() method returns a float. Raspberry Pi 是一个 32 位系统,据我了解, time.clock()方法返回一个浮点数。 Shouldn't the time variable have overflowed only at much larger values, or is the time variable in this case not 32 bits?时间变量不应该只在更大的值时溢出,还是在这种情况下时间变量不是 32 位?

Since then, I've read various threads that indicate time.time() might have been a better method for this use-case, and I might do that in future tests, but I just want to see what I can do with the values I've gathered thus far.从那以后,我阅读了各种线程,这些线程表明time.time()可能是这个用例的更好方法,我可能会在未来的测试中这样做,但我只想看看我可以用这些值做什么到目前为止我已经聚集了。 I believe I can do some processing on the logged time to "de-overflow", for the lack of a better word, and use it as is.我相信我可以对记录的时间进行一些处理以“消除溢出”,因为缺少更好的词,并按原样使用它。 Any thoughts?有什么想法吗?

import time
import csv


def somefunction(someX, someY, csvwriter):
    t = time.clock()

    x = somefunc(someX)
    y = somefunc(someY)

    csvwriter.writerow([t, x, y])

    return
Time (s)时间(秒) X value X值 Y value Y值
2146.978524 2146.978524 -0.0019 -0.0019 0.00032 0.00032
2147.30423 2147.30423 -0.00191 -0.00191 0.00023 0.00023
-2147.336675 -2147.336675 -0.00182 -0.00182 0.00034 0.00034
-2147.000555 -2147.000555 -0.00164 -0.00164 0.00037 0.00037

I doubt this is an 32-bit issue.我怀疑这是一个 32 位问题。 The third bullet point near the beginning of the Python 3.7 documentation of the time module says: time模块的 Python 3.7文档开头附近的第三个要点说:

  • The functions in this module may not handle dates and times before the epoch or far in the future.此模块中的函数可能无法处理纪元之前或未来很远的日期和时间。 The cut-off point in the future is determined by the C library;未来的分界点由C库决定; for 32-bit systems, it is typically in 2038.对于 32 位系统,通常是 2038 年。

That said, I don't really know what the problem is.也就是说,我真的不知道问题是什么。 Perhaps using the time.perf_counter() or time.process_time() functions instead would avoid the issue.也许使用time.perf_counter()time.process_time()函数,而不是将避免这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM