osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why they use this: duration = time.time() - self.start_time + 1


On 04Aug2019 02:33, Hongyi Zhao <hongyi.zhao at gmail.com> wrote:
>I read the code here:
>https://github.com/shichao-an/homura/blob/master/homura.py
>
>It said in line 244:
>  duration = time.time() - self.start_time + 1
>
>I'm very confusing why it used like this instead of the following:
>  duration = time.time() - self.start_time

This is almost certainly because they divide by duration on the next 
line. By adding 1, duration will never be 0.

This code does assume that time.time() never goes backwards, which is 
usually true. However, the system clock can be adjusted, and might go 
backwards.

My personal habit in this circumstance is to compare the duration with 0 
and avoid the division if so; I'd return speed=None in that circumstance 
because I cannot yet compute it. Interestingly you can see that they do 
exactly that logical shuffle on line 248 with the speed. You might 
expect they would do the same for duration but they do not.

Cheers,
Cameron Simpson <cs at cskk.id.au>