Search code examples
pythonalgorithmsortingtime-complexitytime-tracking

Actual time taken by an algorithm to sort


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.


Solution

  • 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")