1

I have a python script than can sort files and i want to know how long it takes to run.

i searched for a module that's names time but it didnt work and it asked me to put a real time.

Julien
  • 13
  • 4
  • 1
    Try the `timeit` module in the standard library – mousetail Feb 24 '23 at 08:45
  • Does this answer your question? [How do I measure elapsed time in Python?](https://stackoverflow.com/questions/7370801/how-do-i-measure-elapsed-time-in-python) – Stephen C Feb 24 '23 at 09:06

2 Answers2

0

You can calculate execution time like that:

import time

start_time = time.time()

# your script here

end_time = time.time()
execution_time = end_time - start_time

print("Execution time:", execution_time, "seconds")
godot
  • 3,422
  • 6
  • 25
  • 42
  • 1
    For rough timing, `time` will work, but for accurate performance measurement, then the `timeit` module is preferred (see [this answer](https://stackoverflow.com/a/17579466/20121320)) – Florent Monin Feb 24 '23 at 08:59
  • thanks, both of ur solutions worked for me – Julien Feb 24 '23 at 13:52
0

I prefer to use time.perf_counter() if we are talking about timing code:

from time import perf_counter

# Get the start 'time'
t1 = perf_counter()

# Some complex time-consuming function
for i in range(10**9):
    pass

# Get the end 'time'
t2 = perf_counter()

# Now you subtract the start time from the end time and you have your elapsed time:
print("Elapsed time:",t2-t1)

Check the documentation here: https://docs.python.org/3/library/time.html#time.perf_counter

Lexpj
  • 921
  • 2
  • 6
  • 15