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 |
相关文章推荐
- C语言中调用lua 脚本执行的效率测试
- python中使用time模块计算代码执行效率的精度测试
- <PY><core python programming笔记>C14 执行环境
- Python几种语句执行效率问题
- python ,shell 脚本 执行同样操作,花费时间效率对比
- 三种语言递归效率的比较matlab,python,c,lisp....
- Cocos Code IDE执行效率太慢,Sublime+QuickXDev依然是最佳的cocos2d-x + Lua编辑器
- Python tricks(6) -- python代码执行的效率
- python学习五:执行linux命令的两种方法、提高运行效率、进制转换、切片详解、ip和mac获取
- 检测Python程序执行效率及内存和CPU使用的7种方法
- 测试递归与循环(这里用for)的执行效率与系统开销
- c++调用lua脚本测试执行效率
- 从 stdin 读取输入的C程序示例 && python脚本调用可执行文件
- C&python文件读写效率
- Python执行系统命令,os.system && os.popen && subprocess.Popen
- ">.NET代码执行效率优化
- erlang +native执行效率果然足以与C语言-O0相媲美
- 利用c模块代码,提升python代码执行效率
- compile vim with lua & python support
- Cocos Code IDE执行效率太慢,Sublime+QuickXDev依然是最佳的cocos2d-x + Lua编辑器