0

Consider the following Python program that performs a lot of disk writing in a for loop:

from time import clock
import os
import sqlite3

data = sqlite3.connect('data.db')


t0 = clock()
with open("data.json") as f:
    for line in f:
        do some operations
        do some sqlite queries
        print(clock()-t0)

Why does the time displayed by print(clock()-t0) does not correspond to the real elapsed time ? It underestimates the elapsed time by a factor ~4.

forpas
  • 160,666
  • 10
  • 38
  • 76
Nichola
  • 135
  • 5

2 Answers2

0

From https://docs.python.org/2.7/library/time.html,

time.clock()
On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

As you can see, it is platform-dependent. A better solution is to use datetime.now()

from datetime import datetime

t0 = datetime.now()
# ... do something ...
elapsed = datetime.now() - t0
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Check `man clock` on the system in question. It probably measures active processor time on Unix-likes, which means times not spent blocked on I/O. It appears to be removed in recent versions of python. – Max Jul 02 '20 at 17:37
-1

try this

from datetime import datetime

t0 =  = datetime.now()
diff = datetime.now()-t0
Alpix
  • 98
  • 1
  • 9