我是靠谱客的博主 传统树叶,这篇文章主要介绍python执行效率最高的是多少级_python执行效率疑问,现在分享给大家,希望可以做个参考。

因为第二种算法是错的,多打印出了许多东西。break 之后合数还是被打印出来了。

PS: 利用 Python 3.2+ 的 functools.lru_cache,可以轻松实现更好的算法。

来,修正后的第二种算法为什么还是比较慢:

>>> time python2 a.py > /dev/null

python2 a.py > /dev/null 1.15s user 0.01s system 99% cpu 1.165 total

>>> time python2 b.py > /dev/null

python2 b.py > /dev/null 1.66s user 0.01s system 99% cpu 1.677 total

>>> time python2 c.py > /dev/null

python2 c.py > /dev/null 1.03s user 0.00s system 99% cpu 1.035 total

>>> time python2 a.py > /dev/null

python2 a.py > /dev/null 1.06s user 0.02s system 98% cpu 1.092 total

>>> time python2 b.py > /dev/null

python2 b.py > /dev/null 1.70s user 0.02s system 99% cpu 1.721 total

>>> time python2 c.py > /dev/null

python2 c.py > /dev/null 1.05s user 0.00s system 99% cpu 1.058 total

第三种算法如下:

def t():

for n in range(1, 200000):

if n <= 1:

continue

if n == 2:

print n

continue

if n % 2 == 0:

continue

i = 3

flag = True

while i * i <= n:

if n % i == 0:

flag = False

break

i += 2

if flag:

print n

t()

查找全局变量比查找局部变量慢。

最后

以上就是传统树叶最近收集整理的关于python执行效率最高的是多少级_python执行效率疑问的全部内容,更多相关python执行效率最高内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部