1

So I am trying to see how long it takes my program to run, and the solution I came up with is:

import datetime

time1 = datetime.datetime.now()

[program code]

time2 = datetime.datetime.now()

print(time2 - time1)

I want to know if this is an efficient/correct solution, because all the guides I could find on the internet would use modules such as timeit, etc.

I would appreciate any feedback.

diens
  • 639
  • 8
  • 26
DavidNiu
  • 13
  • 4
  • Also please [format your code](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – user202729 May 12 '18 at 02:22

1 Answers1

1

Yes, that works. However, another solution is:

import time

start = time.time()

# do stuff

print(time.time() - start)

You could also use time.clock() instead of time.time()

Anish Shanbhag
  • 404
  • 6
  • 15