cpu_intensive.lua
function fib(n)
if n == 0 or n == 1 then
return 1
end
return fib(n-1) + fib(n-2)
end
fib(40)
fib(40)
fib(40)
fib(40)
n = fib(40)
print(n)
cpu_intensive.erl
-module(cpu_intensive).
-compile(export_all).
fib_test() ->
fib(40), fib(40), fib(40), fib(40), fib(40).
fib(0) -> 1;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).
cpu_intensive.py
def fib(n):
if n == 0 or n == 1:
return 1
return fib(n-1) + fib(n-2)
fib(40)
fib(40)
fib(40)
fib(40)
n = fib(40)
print(n)
cpu_intensive.cpp
#include "stdafx.h"
#include <stdio.h>
unsigned int fib(unsigned int n) {
if (n == 0 || n == 1) {
return 1;
}
return fib(n-1) + fib(n-2);
}
int _tmain(int argc, _TCHAR* argv[])
{
fib(40); fib(40); fib(40); fib(40);
unsigned int n = fib(40);
printf("%u", n);
return 0;
}
执行结果:
debug c 45s
release c <2s
erlang 53s
python 4min:18s
pypy 1min:8s
lua 2min:20s
另外有个10亿次数字相加的测试如下:
cputest2.erl
-module(cputest2).
-compile(export_all).
test() ->
add(0).
add(N)
when N > 1000000000 ->
0;
add(N) ->
add(N+1).
cputest2.py
i = 0
while i < 1000000000:
i = i + 1
print(i)
cputest2.lua
i = 0
while i < 1000000000 do
i = i + 1
end
print(i)
cputest2.cpp
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0;
while(i < 1000000000)
i = i + 1;
printf("%d", i);
return 0;
}
执行结果:
python 90s
pypy 13s
erlang 55s
lua 65s
debug c 5s
release c <1s
|
erlang&c&python&lua递归执行效率
最新推荐文章于 2021-05-19 19:09:56 发布