计算Python脚本的执行时间的多种方法

云课堂学Python 2024-04-12 00:49:55

Python 脚本是用于执行特定任务的代码文件。每个 Python 脚本都需要一些时间来执行文件,可以使用以下方法进行计算。

使用time模块

在 Python 中,可以使用 time 模块,测量代码块执行所花费的时间。time 模块的 time() 函数以秒为单位返回时间,计算开始时间和结束时间的差值,得到给定代码块的执行时间。

import timestart_time = time.time()for i in range(1000): print(i)end_time = time.time()execution_time = end_time - start_timeprint("运行时长:",execution_time)使用 timeit 模块

timeit 模块,通过多次运行代码并找到所用时间的平均值,提供了更准确的方法来测量代码片段的执行时间。

import timeitfor i in range(1000): print(i)execution_time = timeit.timeit(number = 50)print("运行时长:",execution_time)使用 datetime 模块

使用 Python 中的 datetime 模块的 datetime.now() 函数记录开始和结束的时间戳,并计算差值来获取代码执行时间。

from datetime import datetimestart_time = datetime.now()for i in range(1000): print(i)end_time = datetime.now()execution_time = (end_time - start_time).total_seconds() print("运行时长:", execution_time)使用 cProfile 模块

cProfile 模块中的 run() 函数,计算每个函数执行所花费的时间以及整个 python 脚本执行时间。

import cProfiledef test(): for i in range(1000): print(i)cProfile.run("test()")

输出:

77004 function calls in 5.191 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.001 0.001 5.191 5.191 7.py:2(test) 1 0.000 0.000 5.191 5.191 <string>:1(<module>) 2000 0.010 0.000 5.167 0.003 backend.py:159(_send_output) 2000 0.001 0.000 0.001 0.000 backend.py:167(_transform_output) 2000 0.007 0.000 0.011 0.000 common.py:189(__init__) 2000 0.005 0.000 0.016 0.000 common.py:205(__init__) 2000 0.002 0.000 0.043 0.000 common.py:217(serialize_message) 2000 0.002 0.000 0.003 0.000 common.py:58(__init__) 2000 0.006 0.000 0.037 0.000 common.py:85(__repr__) 10000 0.010 0.000 0.022 0.000 common.py:87(<genexpr>) 2000 0.002 0.000 0.003 0.000 cpython_backend.py:1098(__getattr__) 2000 0.013 0.000 5.186 0.003 cpython_backend.py:1109(write) 2000 0.015 0.000 5.140 0.003 cpython_backend.py:909(send_message) 2000 0.002 0.000 0.002 0.000 cpython_backend.py:953(_enter_io_function) 2000 0.002 0.000 0.002 0.000 cpython_backend.py:956(_exit_io_function) 2000 0.003 0.000 0.040 0.000 {built-in method builtins.ascii} 1 0.000 0.000 5.191 5.191 {built-in method builtins.exec} 2000 0.001 0.000 0.001 0.000 {built-in method builtins.getattr} 2000 0.001 0.000 0.001 0.000 {built-in method builtins.hasattr} 4000 0.002 0.000 0.002 0.000 {built-in method builtins.isinstance} 2000 0.001 0.000 0.001 0.000 {built-in method builtins.len} 1000 0.004 0.000 5.190 0.005 {built-in method builtins.print} 8000 0.004 0.000 0.004 0.000 {built-in method builtins.repr} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 4000 0.003 0.000 0.003 0.000 {method 'flush' of '_io.TextIOWrapper' objects} 10000 0.010 0.000 0.010 0.000 {method 'format' of 'str' objects} 2000 0.006 0.000 0.028 0.000 {method 'join' of 'str' objects} 2000 0.001 0.000 0.001 0.000 {method 'keys' of 'dict' objects} 2000 0.001 0.000 0.001 0.000 {method 'update' of 'dict' objects} 2000 5.075 0.003 5.075 0.003 {method 'write' of '_io.TextIOWrapper' objects}

文章创作不易,如果您喜欢这篇文章,请关注、点赞并分享给朋友。如有意见和建议,请在评论中反馈!

0 阅读:0

云课堂学Python

简介:感谢大家的关注