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.
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))
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))
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 %
%%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)
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
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')
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
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')
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
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!

Arraysaregenerallymorememory-efficientthanlistsforstoringnumericaldataduetotheirfixed-sizenatureanddirectmemoryaccess.1)Arraysstoreelementsinacontiguousblock,reducingoverheadfrompointersormetadata.2)Lists,oftenimplementedasdynamicarraysorlinkedstruct

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Python lists can store different types of data. The example list contains integers, strings, floating point numbers, booleans, nested lists, and dictionaries. List flexibility is valuable in data processing and prototyping, but it needs to be used with caution to ensure the readability and maintainability of the code.

Pythondoesnothavebuilt-inarrays;usethearraymoduleformemory-efficienthomogeneousdatastorage,whilelistsareversatileformixeddatatypes.Arraysareefficientforlargedatasetsofthesametype,whereaslistsofferflexibilityandareeasiertouseformixedorsmallerdatasets.

ThemostcommonlyusedmoduleforcreatingarraysinPythonisnumpy.1)Numpyprovidesefficienttoolsforarrayoperations,idealfornumericaldata.2)Arrayscanbecreatedusingnp.array()for1Dand2Dstructures.3)Numpyexcelsinelement-wiseoperationsandcomplexcalculationslikemea

ToappendelementstoaPythonlist,usetheappend()methodforsingleelements,extend()formultipleelements,andinsert()forspecificpositions.1)Useappend()foraddingoneelementattheend.2)Useextend()toaddmultipleelementsefficiently.3)Useinsert()toaddanelementataspeci

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

In the fields of finance, scientific research, medical care and AI, it is crucial to efficiently store and process numerical data. 1) In finance, using memory mapped files and NumPy libraries can significantly improve data processing speed. 2) In the field of scientific research, HDF5 files are optimized for data storage and retrieval. 3) In medical care, database optimization technologies such as indexing and partitioning improve data query performance. 4) In AI, data sharding and distributed training accelerate model training. System performance and scalability can be significantly improved by choosing the right tools and technologies and weighing trade-offs between storage and processing speeds.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
