2017 Golang, Python, PHP, C + +, C, Java, Nodejs performance comparison
I in Php/c++/go/py, whim, want to make a simple comparison of the recent mainstream programming language performance, as to how to compare, still have to use the Magic Fibonacci algorithm. It may be more common or fun.
Okay, talk is cheap, show me your code! Turn on your Mac, open Clion and start coding!
1. The first is go, because I personally recently used, feel very good
Package Mainimport "FMT" Func Main () { fmt. Println (Fibonacci)}func Fibonacci (i int) int{ if (i<2) { return i; } Return Fibonacci (I-2) + Fibonacci (i-1);}
Take a look at Go1.7 first:
time Go build fib.go time ./fibgo version go1. 7.5 darwin/amd64real 0m0.206suser 0m0.165ssys 0m0.059s5702887Real 0m0.052suser 0m0.045ssys 0m0.004s
Then, look at the 1.8:
time Go18 build fib.go time ./fibgo version go1. 8 darwin/amd64real 0m0.204suser 0m0.153ssys 0m0.062s5702887Real 0m0.051suser 0m0.045ssys 0m0.003s
The feeling does not see the difference, but the official 1.8 in GC, compile and other aspects of optimization improved by 20%, perhaps the demo is too simple.
2.Python, recently played hot, so take the baby
def Fibonacci (i): if i<2: return I return Fibonacci (i-2) + Fibonacci (i-1) print (Fibonacci (34))
Let's take a look at python2.7
[Email protected]:/works/learncpp$ python2-v && time python2./fib.py
Python 2.7.13
5702887
Real 0m2.651s
User 0m2.594s
SYS 0m0.027s
Then the PY 3.5.
time Python3./3.5. 1 5702887 Real 0m3.110suser 0m2.982ssys 0m0.026s
A glance to see the biggest problem of the PY: The more slow upgrade, but also a lot of bad grammar is not compatible, but usually write algorithms, small programs are good, other times, even if, or use Go bar.
3.PHP Well, I work a lot, so I have to compare
<?phpfunction Fibonacci ($i) { if ($i <2) return $i; Return Fibonacci ($i-2) + Fibonacci ($i-1);} Echo Fibonacci (34);
Because my work mainly uses the php5.4, so first come wave:
[email protected]:/works/learncpp$ php54-v && time php54 fib.php php 5.4 . 43 (CLI) (Built:dec 21 2016 12 : 01 : 59 1997 -2014 the PHP groupzend Engine v2. 4.0 , Copyright (c) 1998 -2014 Zend Technologies 5702887 Span style= "color: #000000;" >real 0m2.288suser 0m2.248ssys 0m0.021s
The test environment is 5.6, so also come wave:
[email protected]:/works/learncpp$ php-v && time php fib.php php 5.6 . 28 (CLI) (Built:dec 6 2016 12 : 38 : 54 1997 -2016 the PHP groupzend Engine v2. 6.0 , Copyright (c) 1998 -2016 Zend Technologies 5702887 Span style= "color: #000000;" >real 0m2.307suser 0m2.278ssys 0m0.017s
New project, oneself play what all is PHP7, see:
[Email protected]:/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 groupzend Engine v3.1.0, Copyright (c) 1998-2017 Zend technologies5702887real 0m0 .815suser 0m0.780ssys 0m0.015s
Feel PHP7 and 5 is a difference, is not a thing at all, and progress is too big, this depends on the bird brother to praise! We recommend that you use PHP7 more.
4.c++ is my favorite theoretical basis, of course, is C++11/14, not the antique C99, etc.
#include <iostream>constexpr int Fibonacci (const int i) { if (i<2) return i; Return Fibonacci (I-2) + Fibonacci (i-1);} int main () { Fibonacci (); return 0;}
First use g++ 6.2 without optimization to see:
time g++-6 -o a.bin Main. CPPtime ./a.bin real 0m0.378suser 0m0.254ssys 0m0.104s5702887 Real 0m0.050suser 0m0.043ssys 0m0.002s
Plus optimized-o2,
time g++-6 -o2-o a.bin Main. CPP time . /a.bin real 0m0.874suser 0m0.344ssys 0m0.180s5702887Real 0m0.034suser 0m0.019ssys 0m0.004s
The effect is still obvious, running only half the time.
5. C also wrote a
#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));}
No optimization:
Time gcc-6 time/c.bin real 0m0.341suser 0m0.063ssys 0m0.101s 5702887Real 0m0.049suser 0m0.044ssys 0m0.002s
Plus-O2 optimization:
Time gcc-6 time/c.bin real 0m0.143suser 0m0.065ssys 0m0.034s 5702887Real 0m0.034suser 0m0.028ssys 0m0.002s
As with c++14, the speed of the optimization is obvious, one times faster.
6.Java, is the last I want to write, although it is very hot, feel too bloated
Class fib{ public static void main (string[] args) { System.out.println (Fibonacci); } static int Fibonacci (int i) { if (i<2) return i; Return Fibonacci (I-2) + Fibonacci (i-1); }}
The result of compiling and running is:
time Javac fib.java && time"1.8.0_25" 1.8. 0_25-25.25-b02, mixed mode) real 0m0.952suser 0m1.302ssys 0m0.144s5702887real 0m0.150suser 0m0.123ssys 0m0.025s
Performance is OK, compile time and c++/go ratio is too low.
7. The final appearance of course is always big purple red JavaScript, no, accurately said is Nodejs (this thing and Java really TMD not half a dime relationship)
function Fibonacci (i) { if (i<2) return i; Return Fibonacci (I-2) + Fibonacci (i-1);} Console.log (Fibonacci (34))
Results:
Time node fib.js V6. 10.0 5702887 Real 0m0.332suser 0m0.161ssys 0m0.062s
The results are still shocking, only TMD 0.3s, a total of less than 0.5s, almost close to Java, but this code volume, maintainability of the advantages of really much thanks to Google Daddy, chromium father's V8 son and open source community.
If Nodejs really run stably, not really can unified "program Lakes", of course, I just said, do not take too seriously.
Let's take a picture:
Summarize:
Feel that each kind of language are different uses, performance is only a very single indicator, I personally value is: readability, maintainability, portability, robustness, extensibility, and then performance. and modern hardware more and more, motionless mobile phone on 8 g,cpu to catch up to 5 years ago the PC CPU, SSD popularization .... I am more optimistic about Golang/php/python, but also focus on modern C + +, such as 14, 17, as for rust, Swift, Java, Scala even if, this is mainly related to personal needs, the company technology stack. Ha ha! Write so much first!
2017 Golang, Python, PHP, C + +, C, Java, Nodejs performance comparison (Golang python php c + + java Nodejs performance)