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
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
This works on Google Colab Notebooks as well.
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 wrap column text in SharePoint Online Modern List Grid View using JSON formatting - SharePoint
- How to track and download Microsoft Teams meeting attendance report of participants/students - Teams
- How to remove Extra Spaces in Notepad++ - NotepadPlusPlus
- Instant Run requires Tools | Android | Enable ADB integration to be enabled - Android-Studio
- Page actions are temporarily disabled [Google Search Console Page Crawling] - Google
- How to Create a Git Branch in 6 Different ways - Git
- SharePoint - The URL is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. - SharePoint
- Detect if Cookies are enabled using JavaScript - JavaScript