Reputation: 1210
I am testing an implementation of Heap using the timeit
module, but I can't get it to print time in float:
def testCustomHeapStartUp(*args):
h = MinHeap(args[0])
return True
if __name__ == "__main__":
for i in range(3):
testList = random.sample(range(100), 100)
print(timeit.Timer("testCustomHeapStartUp(testList)", \
"from __main__ import testCustomHeapStartUp; from __main__ import testList; gc.enabled()"))
This is the output:
<timeit.Timer object at 0x101a58978>
<timeit.Timer object at 0x101a58978>
<timeit.Timer object at 0x101a58978>
Upvotes: 0
Views: 515
Reputation: 69240
timeit.Timer(...)
only creates the instance, it doesn't run and time the code.
You need to call the .timeit()
function on the Timer
instance:
print(timeit.Timer(..., ...).timeit(n))
Where n
is the number of times you want timeit()
to to run the snippet.
Because timeit
is showing up so often, I would refactor your code as:
from timeit import Timer
def testCustomHeapStartUp(*args):
h = MinHeap(args[0])
return True
if __name__ == "__main__":
for i in range(3):
testList = random.sample(range(100), 100)
t = Timer(
"testCustomHeapStartUp(testList)",
"from __main__ import testCustomHeapStartUp; from __main__ import testList; gc.enabled()",
)
print(t.timeit())
Upvotes: 1