筆者采用計算Fibonacci數列第40項的平均用時,在遞歸調用方面來度量語言性能。
筆者所使用的測試平台是安裝Microsoft Windows 10 專業版(版本10.0.18363)操作系統,帶有Inter(R) Core(TM)i7-5600處理器與8GB內存的聯想Thinkpad x250個人筆記本電腦。
from time import *
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
begin = time()
result = fib(40)
end = time()
print('fib(40)=', result)
print('time=',(end-begin)*1000, 'ms')
#!/usr/local/bin/lua
require("os")
function fib(n)
if n <= 2 then
return 1
else
return fib(n -1) + fib(n - 2)
end
end
local start = os.clock()
local result = fib(40)
local end_time = os.clock()
print("fib(40)=", result)
print(string.format("time=%.2fms", (end_time - start)*1000))
import std::IO;
import std::Type;
import std::TIME;
class Test
{
static fib(n->int)->int
{
return n<=2 ? 1 : Test.fib(n-1) + Test.fib(n-2);
}
public static main()
{
start->long = TIME.clock();
result->int = Test.fib(40);
end->long = TIME.clock();
IO.printf("fib(40)=%d\n", Type.get(result));
IO.printf("time=%Dms", Type.get(end-start));
}
}
Python
Lua
Plang
Lua
12170.00ms
12197.00ms
12416.00ms
平均用時:12261.00ms
Plang
15503.00ms
15391.00ms
15414.00ms
平均用時:15436.00ms
Python
23489.57ms
21709.60ms
23390.60ms
平均用時:22863.26ms
方法遞歸調用性能:Python<Plang<Lua
源代碼遠程倉庫:https://github.com/ChivenZhang/plang-demo