Table of Contents
time module
%%time Magic command
line_profiler
memory_profiler
Summary
Home Backend Development Python Tutorial Monitor Python memory usage and code execution time

Monitor Python memory usage and code execution time

Apr 10, 2023 pm 02:56 PM
python Memory develop

Which parts of my code take the longest to run and use the most memory? How can I find areas for improvement?

I’m pretty sure most of us would like to know this during development, and in this article I’ve summarized some ways to monitor the time and memory usage of your Python code.

Monitor Python memory usage and code execution time

This article will introduce 4 methods. The first 3 methods provide time information, and the fourth method can obtain memory usage.

  • time module
  • %%time magic command
  • line_profiler
  • memory_profiler

time module

This is the simplest and most straightforward (but requires manual development) method of calculating how long it takes for your code to run. His logic is also very simple: record the time before and after the code is run, and calculate the difference between the times. This can be achieved as follows:

import time
 
 start_time = time.time()
 result = 5+2
 end_time = time.time()
 
 print('Time taken = {} sec'.format(end_time - start_time))
Copy after login

The following example shows the difference in time between a for loop and a list comprehension:

import time
 
 # for loop vs. list comp
 list_comp_start_time = time.time()
 result = [i for i in range(0,1000000)]
 list_comp_end_time = time.time()
 print('Time taken for list comp = {} sec'.format(list_comp_end_time - list_comp_start_time))
 
 result=[]
 for_loop_start_time = time.time()
 for i in range(0,1000000):
 result.append(i)
 for_loop_end_time = time.time()
 print('Time taken for for-loop = {} sec'.format(for_loop_end_time - for_loop_start_time))
 
 list_comp_time = list_comp_end_time - list_comp_start_time
 for_loop_time = for_loop_end_time - for_loop_start_time
 print('Difference = {} %'.format((for_loop_time - list_comp_time)/list_comp_time * 100))
Copy after login

We all know that for will be slower.

Time taken for list comp = 0.05843973159790039 sec
 Time taken for for-loop = 0.06774497032165527 sec
 Difference = 15.922795107582594 %
Copy after login

%%time Magic command

Magic command is a convenient command built into the IPython kernel that can easily perform specific tasks. Generally, it is used in jupyter notebook.

Add %%time at the beginning of the cell. After the cell execution is completed, the time spent on the cell execution will be output.

%%time
 def convert_cms(cm, unit='m'):
 '''
Function to convert cm to m or feet
'''
 if unit == 'm':
 return cm/100
 return cm/30.48
 
 convert_cms(1000)
Copy after login

The results are as follows:

CPU times: user 24 µs, sys: 1 µs, total: 25 µs
 Wall time: 28.1 µs
 
 Out[8]: 10.0
Copy after login

The CPU times here are the actual time spent by the CPU processing the code, and the Wall time is the real time that the event has passed. At the method entrance and The time between method exits.

line_profiler

The first two methods only provide the total time required to execute the method. Through the time analyzer we can get the running time of each code in the function.

Here we need to use the line_profiler package. Use pip install line_profiler.

import line_profiler
 
 def convert_cms(cm, unit='m'):
 '''
Function to convert cm to m or feet
'''
 if unit == 'm':
 return cm/100
 return cm/30.48
 
 # Load the profiler
 %load_ext line_profiler
 
 # Use the profiler's magic to call the method
 %lprun -f convert_cms convert_cms(1000, 'f')
Copy after login

The output results are as follows:

Timer unit: 1e-06 s
 
 Total time: 4e-06 s
 File: /var/folders/y_/ff7_m0c146ddrr_mctd4vpkh0000gn/T/ipykernel_22452/382784489.py
 Function: convert_cms at line 1
 
 Line # Hits Time Per Hit % Time Line Contents
 ==============================================================
1 def convert_cms(cm, unit='m'):
2 '''
3 Function to convert cm to m or feet
4 '''
5 1 2.0 2.0 50.0 if unit == 'm':
6 return cm/100
7 1 2.0 2.0 50.0 return cm/30.48
Copy after login

You can see that line_profiler provides detailed information about the time spent on each line of code.

  • Line Contents: The code being run
  • Hits: The number of times the line was executed
  • Time: The total time spent (i.e. the number of hits x the number of hits per time )
  • Per Hit: The time it takes for an execution, that is, Time = Hits X Per Hit
  • % Time: The proportion of the total time

You can see Yes, each line of code analyzes the time in detail, which is quite helpful for us to analyze the time.

memory_profiler

Similar to line_profiler, memory_profiler provides line-by-line memory usage of the code.

To install it you need to use pip install memory_profiler. Here we monitor the memory usage of the convert_cms_f function.

from conversions import convert_cms_f
 import memory_profiler
 
 %load_ext memory_profiler
 
 %mprun -f convert_cms_f convert_cms_f(1000, 'f')
Copy after login

The convert_cms_f function is defined in a separate file and then imported. The results are as follows:

Line # Mem usage Increment Occurrences Line Contents
 =============================================================
1 63.7 MiB 63.7 MiB 1 def convert_cms_f(cm, unit='m'):
2 '''
3 Function to convert cm to m or feet
4 '''
5 63.7 MiB 0.0 MiB 1 if unit == 'm':
6 return cm/100
7 63.7 MiB 0.0 MiB 1 return cm/30.48
Copy after login

memory_profiler provides detailed insight into the memory usage of each line of code.

1 MiB (MebiByte) here is almost equal to 1MB. 1 MiB = 1.048576 1MB

But memory_profiler also has some disadvantages: it queries the operating system memory, so the results may be slightly different from the python interpreter. If you run %mprun multiple times in a session, you may notice an increase. The measurement column reports 0.0 MiB for all code lines. This is due to the limitations of magic commands.

Although memory_profiler has some problems, it allows us to clearly understand memory usage and is a very useful tool for development.

Summary

Although Python is not a language known for its execution efficiency, these commands are still very helpful to us in some special cases.


The above is the detailed content of Monitor Python memory usage and code execution time. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles