We can make use of the %time magic command in a Cell of a Jupyter Notebook to measure the execution time of a Python program.
Let us take a look at a few examples.
Example 1: A single Statement in a Cell
%time print(30*4039*3929)
476076930
CPU times: user 199 µs, sys: 189 µs, total: 388 µs
Wall time: 368 µs
![Execution time of a single cell statement - Python Notebook Execution time of a single cell statement - Python Notebook](https://code2care.org/python/measure-execution-time-jupyter-notebook-cell/images/Execution time of a single cell statement - Python Notebook.jpg)
Example 2: Execution Time of a Function Call
def func():
sum = 293*19839*29
print(sum)
We have the function in one cell and we call it from another.
%time func()
168571983
CPU times: user 36 µs, sys: 4 µs, total: 40 µs
Wall time: 42.9 µs
![Execution Time of a Function Call in Jupyter Notebook Execution Time of a Function Call in Jupyter Notebook](https://code2care.org/python/measure-execution-time-jupyter-notebook-cell/images/Execution Time of a Function Call in Jupyter Notebook.jpg)
This works on Google Colab Notebooks as well.
![Know the time of execution of a cell in Google Colab Notebook Example Know the time of execution of a cell in Google Colab Notebook Example](https://code2care.org/python/measure-execution-time-jupyter-notebook-cell/images/Know the time of execution of a cell in Google Colab Notebook Example.jpg)
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- How to turn off Stage Manager - macOS Ventura - MacOS
- How do I get a list of all branches in Git? - Git
- Steps to Enable Developer Mode on iOS 17 - iPhone and iPad - iOS
- Internet Explorer browser auto redirect to Microsoft Edge for compatibility with modern web sites - Microsoft
- Add days/weeks/months/years to LocalDate in Java 8 and above examples - Java
- GitPython: How to check out a Branch - Python
- How to start/boot macOS in safe mode - Big Sur 11.0, Catalina 10.15, or Mojave 10.14 - MacOS
- How to Show File Extensions for Xcode Files - MacOS