且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在python3中循环比python2慢得多

更新时间:2022-02-10 04:08:55

我增加了循环并禁用了输出(显示时速度要慢得多).
而且我不是 Python 专家.但是您可以使用jit编译器pypy来加快python代码的速度,例如(但仍然比luajit慢.)此外,有关python3和python2的主题 可能对你也很有趣.

I've increased your loops and disable the output (it's much slower when it's displayed).
And I'm no python expert. But you can speed up your python code with the jit compiler pypy e.g. (but still slower than luajit.) Furthermore, this topic about python3 and python2 might be interesting for you too.

r=0
for i in range(1,10000):
    for j in range(1,10000):
        r=i*j

python3

$ time python3 loop.py 

real    0m16.612s
user    0m16.610s
sys 0m0.000s

python2

$ time python2 loop.py 

real    0m11.218s
user    0m11.190s
sys 0m0.007s

pypy

$ time pypy loop.py 

real    0m0.923s
user    0m0.900s
sys 0m0.020s

lua

local r=0
for i=1,10000,1 do
    for j=1,10000,1 do
        r=i*j
    end
end

lua 5.2.3

$ time lua loop.lua 

real    0m1.123s
user    0m1.120s
sys 0m0.000s

luajit

$ time luajit loop.lua 

real    0m0.074s
user    0m0.073s
sys 0m0.000s