How to Measure Execution Time in Jupyter Notebook Cell


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

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

This works on Google Colab Notebooks as well.

Know the time of execution of a cell in Google Colab Notebook Example

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

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap