這篇文章主要介紹了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) + fibonacci(i-1); }
先用Go1.7看看:
程式碼如下:
qiangjian@localhost:/works/learnCPP$ go version && time go build fib.go && time ./fibgo version go1.7.5 darwin/amd64 real 0m0.206suser 0m0.165ssys 0m0.059s real 0m0.052suser 0m0.045ssys 0m0.004s
然後,再看看1.8的:
程式碼如下:
##
qiangjian@localhost:/works/learnCPP$ go18 version && time go18 build fib.go && time ./fibgo version go1.8 darwin/amd64 real 0m0.204suser 0m0.153ssys 0m0.062s real 0m0.051suser 0m0.045ssys 0m0.003s
#
def fibonacci(i): if i<2: return i return fibonacci(i-2) + fibonacci(i-1) print(fibonacci(34))
qiangjian@localhost:/works/learnCPP$ python2 -V && time python2 ./fib.py Python 2.7.13 5702887 real 0m2.651s user 0m2.594s sys 0m0.027s
qiangjian@localhost:/works/learnCPP$ python3 -V && time python3 ./fib.py Python 3.5.1 real 0m3.110s user 0m2.982s sys 0m0.026s
#
<?php function fibonacci($i){ if($i<2) return $i; return fibonacci($i-2) + fibonacci($i-1); } echo fibonacci(34);
qiangjian@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 real 0m2.288s user 0m2.248s sys 0m0.021s
qiangjian@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 real 0m2.307s user 0m2.278s sys 0m0.017s
qiangjian@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
#include <iostream> constexpr int fibonacci(const int i){ if(i<2) return i; return fibonacci(i-2) + fibonacci(i-1); } int main() { fibonacci(34); return 0; }
qiangjian@localhost:/works/learnCPP$ time g++-6 -o a.bin main.cpp && time ./a.bin real 0m0.378s user 0m0.254s sys 0m0.104s real 0m0.050s user 0m0.043s sys 0m0.002s
qiangjian@localhost:/works/learnCPP$ time g++-6 -O2 -o a.bin main.cpp && time ./a.bin real 0m0.874s user 0m0.344s sys 0m0.180s real 0m0.034s user 0m0.019s sys 0m0.004s
#include <stdio.h> int fibonacci(int i){ if(i<2) return i; return fibonacci(i-2) + fibonacci(i-1); } int main(){ printf("%d",fibonacci(34)); }
qiangjian@localhost:/works/learnCPP$ time gcc-6 -o c.bin fib.c && time ./c.bin real 0m0.341s user 0m0.063s sys 0m0.101s real 0m0.049s user 0m0.044s sys 0m0.002s
qiangjian@localhost:/works/learnCPP$ time gcc-6 -O2 -o c.bin fib.c && time ./c.bin real 0m0.143s user 0m0.065s sys 0m0.034s real 0m0.034s user 0m0.028s sys 0m0.002s
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) + fibonacci(i-1); } }
qiangjian@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 real 0m0.150s user 0m0.123s sys 0m0.025s
javascript,不,準確地說是Nodejs(這玩意和java真tmd沒半毛錢關係)
#
function fibonacci(i){ if(i<2) return i; return fibonacci(i-2) + fibonacci(i-1); } console.log(fibonacci(34))
qiangjian@localhost:/works/learnCPP$ node -v && time node fib.js v6.10.0 real 0m0.332s user 0m0.161s sys 0m0.062s
swift、java、scala就算了,這主要和個人的需要、公司技術棧相關。哈哈! 先寫這麼多吧!
以上是具體分析golang、python、php、c++、c、java、Nodejs效能比較的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!