Stay Hungry,Stay Foolish!

python应用耗时分析

Linux time command

https://linuxize.com/post/linux-time-command/

粗略分析整个程序的耗时情况。

 

time wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.19.9.tar.xz

What will be printed as an output depends on the version of the time command you’re using:

# Bash
real	0m33.961s
user	0m0.340s
sys	0m0.940s

# Zsh
0.34s user 0.94s system 4% cpu 33.961 total

# GNU time (sh)
0.34user 0.94system 0:33.96elapsed 4%CPU (0avgtext+0avgdata 6060maxresident)k
0inputs+201456outputs (0major+315minor)pagefaults 0swaps
  • real or total or elapsed (wall clock time) is the time from start to finish of the call. It is the time from the moment you hit the Enter key until the moment the wget command is completed.
  • user - amount of CPU time spent in user mode.
  • system or sys - amount of CPU time spent in kernel mode.

 

Python Inner Method

https://realpython.com/python-profiling/#timeit-benchmark-short-code-snippets

time: Measure the Execution Time

使用time模块,

time.perf_counter() 获取程序经历时间

time.process_time() 获取CPU处理时间

 

好处: 对可疑代码做定制测量。

import time

def sleeper():
    time.sleep(1.75)


def spinlock():
    for _ in range(100_000_000):
        pass


for function in sleeper, spinlock:
    t1 = time.perf_counter(), time.process_time()
    function()
    t2 = time.perf_counter(), time.process_time()
    print(f"{function.__name__}()")
    print(f" Real time: {t2[0] - t1[0]:.2f} seconds")
    print(f" CPU time: {t2[1] - t1[1]:.2f} seconds")
    print()

 

timeit: Benchmark Short Code Snippets

对可疑函数做定制测量,

与time模块相比,不用手写测时语句。

from timeit import timeit

def fib(n):
    return n if n < 2 else fib(n - 2) + fib(n - 1)


iterations = 100
total_time = timeit("fib(30)", number=iterations, globals=globals())

f"Average time is {total_time / iterations:.2f} seconds"

 

cProfile: Collect Detailed Runtime Statistics

可以统计每个函数的调用次数,单次耗时,总耗时。

from cProfile import Profile
from pstats import SortKey, Stats

def fib(n):
    return n if n < 2 else fib(n - 2) + fib(n - 1)


with Profile() as profile:
    print(f"{fib(35) = }")
    (
        Stats(profile)
        .strip_dirs()
        .sort_stats(SortKey.CALLS)
        .print_stats()
    )

 

Profile参考

https://docs.python.org/3/library/profile.html


 

posted @ 2023-08-26 10:33  lightsong  阅读(36)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel