Skip to content Skip to sidebar Skip to footer

Why Time.time() Gives 0.0?

I have a python program defined by a function myFunc(m,n) Basically, the function contains two for loops. def myFunc(m, n) : for i in range(m) : for j in range(n) :

Solution 1:

Because time.time() - time.time() will be 0.0 when myFunc doesn't take very long.

Solution 2:

Per the time.time docs,

even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.

So if myFunc takes less than a second and you are using such an OS, then the difference between two calls of time.time could be 0.0.

You could use timeit.default_timer instead of time.time. timeit.default_timer will choose the best timer (time.time, time.clock or time.perf_counter) for your system.

Also note that the timeit module offers more accurate ways to test the performance of code snippets.

Post a Comment for "Why Time.time() Gives 0.0?"