Sample code that specifically analyzes performance comparisons of Golang, Python, PHP, C + +, C, Java, Nodejs

Source: Internet
Author: User
This article mainly introduces Golang, Python, PHP, C + +, C, Java, Nodejs Performance comparison of relevant data, the need for friends can refer to the following

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:

The code is as follows:

qiangjian@localhost:/works/learncpp$ Go version && time go build  fib.go  && time/fibgo version go1.7.5 darwin/amd64real    0m0.206suser    0m0.165ssys     0m0.059sreal    0m0.052suser    0m0.045ssys     0m0.004s

Then, look at the 1.8:

The code is as follows:

qiangjian@localhost:/works/learncpp$ go18 version && time go18 build  fib.go  && time./fibgo Version go1.8 darwin/amd64real    0m0.204suser    0m0.153ssys     0m0.062sreal    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


qiangjian@localhost:/works/learncpp$ python2-v && time python2./fib.py Python 2.7.135702887real 0m2.651suser 0m2.594ssys 0m0.027s

Then the PY 3.5.


qiangjian@localhost:/works/learncpp$ python3-v && time python3./fib.py Python 3.5.1real  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:


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 groupzend Engine v2.4.0, Copyright (c) 1998-2014 Zend technologiesreal 0m2.288suser
  0m2.248ssys   0m0.021s

The test environment is 5.6, so also come wave:


qiangjian@localhost:/works/learncpp$ php-v && time php fib.php php 5.6.28 (CLI) (Built:dec 6 12:38:54) Copy Right (c) 1997-2016 the PHP groupzend Engine v2.6.0, Copyright (c) 1998-2016 Zend technologiesreal  0m2.307suser  0m 2.278ssys   0m0.017s

New project, oneself play what all is PHP7, see:


qiangjian@localhost:/works/learncpp$ php-v && time php fib.phpphp 7.1.2 (CLI) (Built:feb 10:52:17) (NTS Copyright (c) 1997-2017 the PHP groupzend Engine v3.1.0, Copyright (c) 1998-2017 Zend technologies5702887real 0m0.81  5suser  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:


qiangjian@localhost:/works/learncpp$ time G++-6-o a.bin main.cpp && time/a.bin real  0m0.378suser  0m0.254ssys   0m0.104sreal  0m0.050suser  0m0.043ssys   0m0.002s

Plus optimized-o2,


qiangjian@localhost:/works/learncpp$ time G++-6-o2-o a.bin main.cpp && time/a.bin Real  0m0.874suser
  0m0.344ssys   0m0.180sreal  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:


qiangjian@localhost:/works/learncpp$ time Gcc-6-o c.bin fib.c && time/c.bin real  0m0.341suser  0m0.063ssys   0m0.101sreal  0m0.049suser  0m0.044ssys   0m0.002s

Plus-O2 optimization:


qiangjian@localhost:/works/learncpp$ time gcc-6-o2-o c.bin fib.c && time./c.bin Real  0m0.143suser  0m0.065ssys   0m0.034sreal  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:


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.952suser  0m1.302ssys   0m0.144sreal  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:


qiangjian@localhost:/works/learncpp$ node-v && Time node fib.js v6.10.0real  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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.