Home » Python » Python programs

# Program to find the execution time of a program in Python

In this tutorial, we will learn **how to find the execution time of a simple program that will calculate the factorial of a large number in the Python programming language?**

Submitted by Bipin Kumar, on November 09, 2019

The **execution time of a program** is defined as the time spent by the system to execute the task. As we all know any program takes some **execution time** but we don't know how much. So, don't worry, in this tutorial we will learn it by using the datetime module and also we will see the execution time for finding the factorial of a large number. A large number will be provided by the user and we have to calculate the factorial of a number, also we have to **find the execution time of the factorial program**. Before going to write the Python program, we will try to understand the algorithm.

**Algorithm to find the execution time of a factorial program:**

- Initially, we will import the datetime module and also the math module(to find the factorial) in the Program. Take the value of a number N from the user.
- Take the value of a number N from the user.
- Find the initial time by using now() function and assign it to a variable which is t_start.
- Calculate the factorial of a given number(N) and print it.
- Here, we will also find the current time and assign it to a variable which is t_end.
- To know the execution time simply find the difference between the t_end and t_start i.e t_end - t_start.

Now, let's start writing the Python program by simply implementing the above algorithm.

# importing the modules from datetime import datetime import math N=int(input("Enter the value of N: ")) t_start=datetime.now() s=math.factorial(N) print("factorial of the number:",s) t_end=datetime.now() e=t_end-t_start print("The execution time for factorial program: ",e)

**Output**

Enter the value of N: 25 factorial of the number: 15511210043330985984000000 The execution time for factorial program: 0:00:00.000022

The output format of the **execution time** of factorial as **"hours: minutes: seconds. microseconds"**.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions