Friday, October 29, 2021

[SOLVED] Time variable in script became negative during execution

Issue

I have a Python program running on my Raspberry Pi 3B doing a bunch of image processing and so on. 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. 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). 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. Shouldn't the time variable have overflowed only at much larger values, or is the time variable in this case not 32 bits?

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. 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 Y value
2146.978524 -0.0019 0.00032
2147.30423 -0.00191 0.00023
-2147.336675 -0.00182 0.00034
-2147.000555 -0.00164 0.00037

Solution

I doubt this is an 32-bit issue. The third bullet point near the beginning of the Python 3.7 documentation of the time module says:

  • 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; for 32-bit systems, it is typically in 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.



Answered By - martineau