How to get current CPU and RAM usage in Python?

前端 未结 16 1512
攒了一身酷
攒了一身酷 2020-11-22 04:09

What\'s your preferred way of getting current system status (current CPU, RAM, free disk space, etc.) in Python? Bonus points for *nix and Windows platforms.

There s

16条回答
  •  失恋的感觉
    2020-11-22 04:56

    To get a line-by-line memory and time analysis of your program, I suggest using memory_profiler and line_profiler.

    Installation:

    # Time profiler
    $ pip install line_profiler
    # Memory profiler
    $ pip install memory_profiler
    # Install the dependency for a faster analysis
    $ pip install psutil
    

    The common part is, you specify which function you want to analyse by using the respective decorators.

    Example: I have several functions in my Python file main.py that I want to analyse. One of them is linearRegressionfit(). I need to use the decorator @profile that helps me profile the code with respect to both: Time & Memory.

    Make the following changes to the function definition

    @profile
    def linearRegressionfit(Xt,Yt,Xts,Yts):
        lr=LinearRegression()
        model=lr.fit(Xt,Yt)
        predict=lr.predict(Xts)
        # More Code
    

    For Time Profiling,

    Run:

    $ kernprof -l -v main.py
    

    Output

    Total time: 0.181071 s
    File: main.py
    Function: linearRegressionfit at line 35
    
    Line #      Hits         Time  Per Hit   % Time  Line Contents
    ==============================================================
        35                                           @profile
        36                                           def linearRegressionfit(Xt,Yt,Xts,Yts):
        37         1         52.0     52.0      0.1      lr=LinearRegression()
        38         1      28942.0  28942.0     75.2      model=lr.fit(Xt,Yt)
        39         1       1347.0   1347.0      3.5      predict=lr.predict(Xts)
        40                                           
        41         1       4924.0   4924.0     12.8      print("train Accuracy",lr.score(Xt,Yt))
        42         1       3242.0   3242.0      8.4      print("test Accuracy",lr.score(Xts,Yts))
    

    For Memory Profiling,

    Run:

    $ python -m memory_profiler main.py
    

    Output

    Filename: main.py
    
    Line #    Mem usage    Increment   Line Contents
    ================================================
        35  125.992 MiB  125.992 MiB   @profile
        36                             def linearRegressionfit(Xt,Yt,Xts,Yts):
        37  125.992 MiB    0.000 MiB       lr=LinearRegression()
        38  130.547 MiB    4.555 MiB       model=lr.fit(Xt,Yt)
        39  130.547 MiB    0.000 MiB       predict=lr.predict(Xts)
        40                             
        41  130.547 MiB    0.000 MiB       print("train Accuracy",lr.score(Xt,Yt))
        42  130.547 MiB    0.000 MiB       print("test Accuracy",lr.score(Xts,Yts))
    

    Also, the memory profiler results can also be plotted using matplotlib using

    $ mprof run main.py
    $ mprof plot
    

    Note: Tested on

    line_profiler version == 3.0.2

    memory_profiler version == 0.57.0

    psutil version == 5.7.0


    EDIT: The results from the profilers can be parsed using the TAMPPA package. Using it, we can get line-by-line desired plots as plot

提交回复
热议问题