Actual time taken by an algorithm to sort

294 Views Asked by At

Can one find the actual time taken by a code to run using a type of algorithm (bubble sort, binary sort, etc)?I know about the time complexity and the "Big O" but can we actually find the exact time (in seconds and minutes) taken by our code (and not like by using a stopwatch but by a set of code which will print out the time taken by our code to run)? If yes can someone tell me how.

1

There are 1 best solutions below

0
On BEST ANSWER

In Python, you can do this:

from timeit import default_timer as timer

def yourFunction():
    print("Something")

start = timer()
yourFunction()
end = timer()
print("Execution Time:", round(end - start, 2), "seconds")