-2

I have a piece of code where I have calulated the time taken for the algorithm to run through datetime as below.

begin_time = datetime.datetime.now()
#some algo code
datetime.datetime.now()-begin_time

I expected the output in seconds but it is in some different form as below:

0.000359

I am pretty sure that these are not seconds. How do i convert it to seconds?

AMC
  • 2,642
  • 7
  • 13
  • 35
Shivam Sarda
  • 37
  • 2
  • 9

2 Answers2

1

Try this

import time

tic = time.perf_counter()

#Your code here

toc = time.perf_counter()
print(f`'Your program took {toc - tic:0.2f} seconds to run')
JaniniRami
  • 493
  • 3
  • 11
0

The difference is calculated in hours so you just need to multiply it by 3600 to convert it to seconds.

Shivam Sarda
  • 37
  • 2
  • 9