Search code examples
python-3.xbenchmarking

How do you create a timing script that you can end by changing a variable?


Using: Python 3.7 on Windows 10

I am making an algorithm that solves mazes. I want to be able to start a timer script when I start the algorithm, and stop when I change a variable, say 'stop'.

The structure of my program is as follows:

import dependencies
algorithm 1
algorithm 2
algorithm 3
algorithm 4
solution
results

On the results screen, the times for each algorithm would be displayed.

Could anyone please point me in the right direction to creating such a timing script? Thx!

P.S. If you wish for more details about the circuit, feel free to ask questions.


Solution

  • You could use datetime.now to get the time delta:

    from datetime import datetime
    
    start = datetime.now()
    your_algorithm()
    delta = datetime.now() - start
    

    Considering that your algorithm interrupts the execution when the stop flag is raised.