fib函数的性能测试:pikapython的性能太差了,比lua5.3差上百倍多。
平台都是用win10 VS2017编译的pikapython1.13 lua5.3.6 x64 release
----------------------------pikapython1.13代码:------------------------------------
import time
def fib_recursive(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib_recursive(n-1) + fib_recursive(n-2)
tmstart = time.tick_ms()
fib_recursive(25)
tmend = time.tick_ms()
print(tmend - tmstart)
---------------------------lua代码:----------------------------------
function fibonacci(n)
if n <= 1 then
return n
else
return fibonacci(n-1) + fibonacci(n-2)
end
end
function measure_fib_time(n)
local startTime = os.clock()
local result = fibonacci(n)
local endTime = os.clock()
-- os.clock() 返回的是秒数,乘以1000转换为毫秒
local elapsedTime = (endTime - startTime) * 1000
return result, elapsedTime
end
-- 示例:计算第30个斐波那契数及其耗时
local result, timeTaken = measure_fib_time(25)
print("Fibonacci(25) =", result, "Time taken:", timeTaken, "ms")
===============pikapython运行结果:ms毫秒=================
C:\Users\Administrator\Downloads>pika fib.py
~~~/ POWERED BY \~~~
~ pikapython.com ~
~~~~~~~~~~~~~~~~~~~~
(pikascript) pika compiler:
compiling fib.py...
compiling time.py...
linking pikascript-api/pikaModules_cache.py.a...
(pikascript) all succeed.
16972
================lua运行结果:毫秒========================
C:\Users\Administrator\Downloads\>lua53.exe fib.lua
Fibonacci(25) = 75025 Time taken: 13.0 ms
pikapython的vm还有很大的优化空间。。。。。。。。。。。。。。。。
离线