Simple way to measure cell execution time in ipython notebook
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
In the realm of data analysis and scientific computing, accurately measuring the execution time of code is crucial for performance tuning and optimization. This is particularly important in interactive environments like IPython notebooks, where users often explore and experiment with code iteratively. This article delves into a straightforward method for measuring cell execution time in an IPython notebook, providing you with technical explanations, examples, and additional insights for a comprehensive understanding.
Timing Code Execution with `%%time` and `%%timeit` Magic Commands
IPython offers powerful built-in magic commands to facilitate the measurement of execution time: `%%time` and `%%timeit`. These magic commands are specifically designed for measuring the time taken by code cells.
1. `%%time` Magic Command
The `%%time` magic command is used to measure the wall time taken to execute a single run of a code cell. This is useful when you're interested in knowing how long a particular snippet takes to execute in real-world scenarios.
Example:
- Wall time: The actual elapsed time.
- CPU time: The time taken by the CPU to execute the code.
- Wall Time: This is the total time taken from start to end of the execution, including time slices used by other processes and time the system spends in other tasks.
- CPU Time: The time for which a CPU was actively working on the particular task.
- The `%%timeit` command automatically executes the contained code several times (the number of iterations depends on the execution time itself to ensure statistical significance) and provides the average time taken per execution.
- It also determines the standard deviation, allowing users to understand the variability in run time.
- System load: Other processes running on your system can influence execution time, especially for wall time measurements.
- Caching and warm-up: The Python interpreter or external libraries might cache results, affecting the execution time of subsequent runs.
- Consider running time measurements multiple times to average out anomalies.
- Benchmark after ensuring minimal system load for more consistent results.
Related reading
- Simple way to visualize a TensorFlow graph in Jupyter?
- Simplest feature selection algorithm
- Sklearn Categorical Imputer?
- Sklearn Chi2 For Feature Selection
- Simplifying expression trees
- SimpMessagingTemplate.convertAndSend with RabbitMQ works very slow
- Simpler way to avoid the UserWarning Converting sparse IndexedSlices
- Simpler way to create dictionary of separate variables?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.