Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python detect the running time and memory consumption of a program?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Python how to detect the running time of the program and the relevant knowledge of the memory occupation, the content is detailed and easy to understand, the operation is simple and fast, and has certain reference value. I believe that everyone will have some gains after reading this Python how to detect the running time of the program and the memory occupation. Let's take a look at it together.

Related Library Time

The time library is Python's standard library for processing time, providing the ability to obtain system time and format output, providing system-level precision timing for program performance analysis.

common methods

Time to get time()

time format strftime(format)

sleep(seconds)

OS

The os library is one of Python's standard libraries that provides common, basic operating system interaction capabilities.

common methods

Open file os.open ()

Return to current working directory os.getcwd()

Get the process ID of the current process os.getpid

psutil

psutil is an open-source, platform-extensible library that provides convenient functions for accessing system information such as CPU, memory, network, disk, etc. It can also be used for process management.

common methods

CPU_count()

Get the current Process object Process()

View network card information statistics net_io_counters()

Code Example:

Next, we will write two functions to check the runtime and memory usage of the program. Also write a simple function to test. The detailed codes are as follows:

import time

import os

import psutil

def count_time(func):

def int_time():

start_time = time.time()

func()

over_time = time.time()

total_time = over_time - start_time

print("Program has been running for %s seconds" % total_time)

return int_time

def count_info(func):

def float_info():

pid = os.getpid()

p = psutil.Process(pid)

info_start = p.memory_full_info().uss/1024

func()

info_end=p.memory_full_info().uss/1024

print("Program uses memory"+str(info_end-info_start)+"KB")

return float_info

@count_time

@count_info

def main():

a = [i for i in range(10000)]

print(a)

if __name__ == '__main__':

main()

The content of this article on "How Python detects the running time and memory consumption of programs" is introduced here. Thank you for reading! I believe everyone has a certain understanding of "Python how to detect the running time and memory occupation of programs." If you want to learn more, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report