How To Increase Execution Time In Python

In this tutorial, we will discuss how to increase the execution time in Python. This can be helpful when running resource-intensive applications or when the task requires more time to complete than allowed by the system default settings.

The primary focus will be on using the time module and making use of the **sys.setrecursionlimit()** method to increase the maximum recursion depth.

Step 1: Increase the recursion limit

In Python, the recursion limit determines the maximum depth of nested recursion calls. This is crucial in stopping the program from entering an infinite loop and causing a StackOverflowError. By default, it is set to 3000. To increase the recursion limit, you can make use of the sys.setrecursionlimit() method.

In the above code, we have increased the recursion limit to 10000. You can set it to any value based on your requirements.

Step 2: Adding sleep functionality to your program

The **time module** in Python provides various time-related functions, including the time.sleep(). This function can be used to pause the execution of a program for a given number of seconds.

To implement a sleep in your Python script:

In the above example, the script will pause for 5 seconds between printing “Starting…” and “…Finished”.

Step 3: Using a Timer to schedule function calls

To precisely control the execution time of a function, you can use the threading.Timer class from the Python **threading module**.

Here’s an example of how to use threading.Timer:

In this example, the delayed_function will be executed after a 10-second delay from the start of the timer.

Full code

Waiting for the delayed function to be called
Function executed after a delay


In this tutorial, we’ve covered how to increase the execution time in Python by modifying the recursion limit, adding sleep functionality, and using a Timer to schedule function calls. These techniques can be utilized to control and optimize the execution time of your Python scripts as per your requirements.