Python 官方文档:入门教程 => 点击学习
在python中加快函数运行速度的方法有以下几种使用内置函数,去掉属性访问from math import sqrt # 直接引用特定函数或属性import timedef func():lst = []for i in range(1,
在python中加快函数运行速度的方法有以下几种
使用内置函数,去掉属性访问
from math import sqrt # 直接引用特定函数或属性
import time
def func():
lst = []
for i in range(1, 10000000):
lst.append(sqrt(i)) # 直接调用 sqrt
return lst
start = time.time()
lst = func()
end = time.time()
print(end-start)
使用函数生成器
from math import sqrt
import time
def func():
# for 循环改为列表推导式
lst = [sqrt(i) for i in range(1, 10000000)]
return lst
start = time.time()
lst = func()
end = time.time()
print(end-start)
--结束END--
本文标题: python怎么加快函数运行速度
本文链接: https://www.lsjlt.com/news/113970.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0