原标题:golang、python、php、c++、c、java性能对比
2017年的golang、python、php、c++、c、java、Nodejs性能对比
本人在PHP/C++/Go/Py时,突发奇想,想把最近主流的编程语言性能作个简单的比较, 至于怎么比,还是不得不用神奇的斐波那契算法。可能是比较常用或好玩吧。
好了,talk is cheap, show me your code! 打开Mac,点开Clion开始Coding吧!
1.怎么第一是Go呢,因为我个人最近正在用,感觉很不错
package main import "fmt" func main{ fmt.Println(fibonacci(34)) } func fibonacci(i int) int{ if(i<2){ return i; } return fibonacci(i-2) &#43; fibonacci(i-1); }
先用 Go1.7看看&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ go version && time go build fib.go && time ./fib go version go1.7.5 darwin/amd64 real 0m0.206s user 0m0.165s sys 0m0.059s 5702887 real 0m0.052s user 0m0.045s sys 0m0.004s
然后&#xff0c;再看看1.8的&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ go18 version && time go18 build fib.go && time ./fib go version go1.8 darwin/amd64 real 0m0.204s user 0m0.153s sys 0m0.062s 5702887 real 0m0.051s user 0m0.045s sys 0m0.003s
感觉看不出差异&#xff0c;但官方1.8在GC、Compile等方面优化提升了20%&#xff0c;可能是这demo太简单了吧。
2.Python&#xff0c;最近玩得也火热&#xff0c;所以拿来比比
def fibonacci(i): if i<2: return i return fibonacci(i-2) &#43; fibonacci(i-1) print(fibonacci(34))
先来看看python2.7
qiangjian&#64;localhost:/works/learnCPP$ python2 -V && time python2 ./fib.py
Python 2.7.13
5702887
real 0m2.651s
user 0m2.594s
sys 0m0.027s
接着是Py 3.5
qiangjian&#64;localhost:/works/learnCPP$ python3 -V && time python3 ./fib.py Python 3.5.1 5702887 real 0m3.110s user 0m2.982s sys 0m0.026s
一眼就看出Py最大的问题了&#xff1a;越升级越慢&#xff0c; 而且要命的是很多语法不兼容&#xff0c;但平时写写算法、小程序还不错&#xff0c;其它的时候&#xff0c;就算了&#xff0c;还是用Go吧。
3.PHP嘛&#xff0c;我工作用的多&#xff0c;所以也必须比较下
由于我的工作主要用的php5.4&#xff0c;所以先来波&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ php54 -v && time php54 fib.php PHP 5.4.43 (cli) (built: Dec 21 2016 12:01:59) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies 5702887 real 0m2.288s user 0m2.248s sys 0m0.021s
测试环境是5.6&#xff0c;所以也来波&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ php -v && time php fib.php PHP 5.6.28 (cli) (built: Dec 6 2016 12:38:54) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies 5702887 real 0m2.307s user 0m2.278s sys 0m0.017s
新项目、自己玩什么的都是php7, 请看&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ php -v && time php fib.php PHP 7.1.2 (cli) (built: Feb 17 2017 10:52:17) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies 5702887 real 0m0.815s user 0m0.780s sys 0m0.015s
感觉php7和5是天壤之别&#xff0c;完全不是一个东西&#xff0c;而且进步提升太大了&#xff0c;在此依赖鸟哥点个赞&#xff01;建议大家多用php7.
4.C&#43;&#43;是我最爱的理论基础&#xff0c;当然说的是C&#43;&#43;11/14&#xff0c;不是老古董c99等
#include constexpr int fibonacci(const int i){ if(i<2) return i; return fibonacci(i-2) &#43; fibonacci(i-1); } int main { fibonacci(34); return 0; }
先用g&#43;&#43; 6.2 无优化看看&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ time g&#43;&#43;-6 -o a.bin main.cpp && time ./a.bin real 0m0.378s user 0m0.254s sys 0m0.104s 5702887 real 0m0.050s user 0m0.043s sys 0m0.002s
加优化-O2后&#xff0c;
qiangjian&#64;localhost:/works/learnCPP$ time g&#43;&#43;-6 -O2 -o a.bin main.cpp && time ./a.bin real 0m0.874s user 0m0.344s sys 0m0.180s 5702887 real 0m0.034s user 0m0.019s sys 0m0.004s
效果还是很明显的&#xff0c; 运行时间只有前者一半。
5. C也写了个
#include int fibonacci(int i){ if(i<2) return i; return fibonacci(i-2) &#43; fibonacci(i-1); } int main{ printf("%d",fibonacci(34)); }
不加优化&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ time gcc-6 -o c.bin fib.c && time ./c.bin real 0m0.341s user 0m0.063s sys 0m0.101s 5702887 real 0m0.049s user 0m0.044s sys 0m0.002s
加-O2优化:
qiangjian&#64;localhost:/works/learnCPP$ time gcc-6 -O2 -o c.bin fib.c && time ./c.bin real 0m0.143s user 0m0.065s sys 0m0.034s 5702887 real 0m0.034s user 0m0.028s sys 0m0.002s
和C&#43;&#43;14一样&#xff0c; 优化后提速明显&#xff0c;快了一倍。
6.Java&#xff0c;是我最不想写的&#xff0c;虽然它很火&#xff0c;感觉太臃肿了
class Fib{ public static void main(String[] args){ System.out.println(fibonacci(34)); } static int fibonacci( int i){ if(i<2) return i; return fibonacci(i-2) &#43; fibonacci(i-1); } }
编译、运行的结果是&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ java -version && time javac Fib.java && time java Fib java version "1.8.0_25" Java(TM) SE Runtime Environment (build 1.8.0_25-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode) real 0m0.952s user 0m1.302s sys 0m0.144s 5702887 real 0m0.150s user 0m0.123s sys 0m0.025s
性能还行&#xff0c; Compile时间和c&#43;&#43;/go比就太low了。
7.最后出场的当然是一直大紫大红的java&#xff0c;不&#xff0c;准确说是Nodejs(这玩意和java真tmd没半毛钱关系)
function fibonacci(i){ if(i<2) return i; return fibonacci(i-2) &#43; fibonacci(i-1); } console.log(fibonacci(34))
结果&#xff1a;
qiangjian&#64;localhost:/works/learnCPP$ node -v && time node fib.js v6.10.0 5702887 real 0m0.332s user 0m0.161s sys 0m0.062s
结果还是令人震惊的&#xff0c;竟然只有TMD 0.3s&#xff0c;一共也不到0.5s&#xff0c;几乎接近java了&#xff0c; 但这代码量、维护性的优势还真得多感谢Google爹、Chromium爹的V8儿子以及开源社区。
如果Nodejs真的运行得稳定了&#xff0c; 不准真能一统“程序江湖”&#xff0c; 当然我只是说说&#xff0c;不要太认真。
来张图中吧&#xff1a;
总结&#xff1a;
责任编辑&#xff1a;