Yes, Benjamin! In Jupyter Notebook, you can use several tools and techniques to identify and sort the most time-consuming instructions (code cells). Here are some practical approaches:
%time and %timeit%time: Measures the execution time of a single statement.
python%time your_function_or_code()
%timeit: Runs the code multiple times and provides an average execution time, which is useful for small, fast-running code.
python%timeit your_function_or_code()
%lprunline_profiler package to profile each line of a function.bashpip install line_profiler
python%load_ext line_profiler %lprun -f your_function your_function()
%pruncProfile module to profile the entire notebook or a specific function.
python%prun your_function()
bashjupyter nbextension install --py execute_time
pythonimport time start = time.time() # Your code here end = time.time() print(f"Execution time: {end - start:.2f} seconds")
cProfile for a more interactive analysis.
bashpip install snakeviz
bashpip install py-spy py-spy top --pid <your_python_process_id>
Would you like a step-by-step guide for any of these methods, or do you have a specific use case in mind?
In Jupyter Notebook, the Cell Tools menu is part of the classic notebook interface (not JupyterLab) and provides additional options for working with individual cells. However, I realize now that the "Run with Profiler" option is not a default feature in the standard Jupyter Notebook interface—my apologies for the confusion.
Instead, the profiling features I mentioned earlier (like %prun, %lprun, and %timeit) are typically accessed via magic commands in the notebook cells themselves, not through a dedicated "Cell Tools" menu.
Magic Commands:
%time, %timeit, %prun, and %lprun directly in a cell to profile code execution.Extensions:
Third-party Tools:
snakeviz or py-spy are used outside the notebook for deeper profiling.If you’re using JupyterLab, you can also install extensions like @jupyterlab/execute-time to display cell execution times.
Would you like help setting up any of these tools or extensions? Or are you looking for something specific in the Jupyter interface?