您的位置:首页 > 编程语言 > Lua

erlang&c&python&lua递归执行效率

2015-02-05 19:21 351 查看
[align=center][/align]

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",
d7ba
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 protobuf python