How to Measure Cell Execution Time in an IPython Notebook: A Simple Guide?

Estimated read time 2 min read

To measure the execution time of a cell in an IPython Notebook, you can use the %%time cell magic command. Here’s an example of how to use it:

%%time

# Your code here
for i in range(1000000):
    pass

In this example, the %%time command is placed at the beginning of the cell, followed by the code to be executed. When the cell is run, IPython will print the execution time in seconds, along with other information like CPU and memory usage.

If you want to measure the execution time of multiple cells in a notebook, you can use the %time line magic command instead. Here’s an example of how to use it:

# Your code here
for i in range(1000000):
    pass
%time print('Hello, world!')

In this example, the %time command is placed before the line of code to be executed. When the cell is run, IPython will print the execution time of the line in seconds.

Note that the execution time can vary depending on factors like CPU speed, memory usage, and system load. To get more accurate timing results, you may want to run the cell or line multiple times and take an average. You can do this by adding the -n flag to the %time or %%time command, followed by the number of times to run the code. For example:

%%time -n 10

# Your code here
for i in range(1000000):
    pass

In this example, the code inside the cell will be run 10 times, and IPython will print the average execution time.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply