Detailed analysis of sample code for golang, python, php, c ++, c, java, and Nodejs performance comparison

Source: Internet
Author: User
This article describes performance comparison of golang, python, php, c ++, c, java, and Nodejs, for more information about golang, python, php, c ++, c, java, and Nodejs performance comparison

When I was in PHP/C ++/Go/Py, I had a whim and wanted to make a simple comparison of the performance of the latest mainstream programming languages, we still have to use the magic Fibonacci algorithm. It may be common or entertaining.

Okay, talk is cheap, show me your code! Open Mac and click Clion to start Coding!

1. how should I Go first? because I am using it recently, I feel very good.


package mainimport "fmt"func main(){  fmt.Println(fibonacci(34))}func fibonacci(i int) int{  if(i<2){    return i;  }  return fibonacci(i-2) + fibonacci(i-1);}

First check with Go1.7:

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, let's take a 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

I don't think there is any difference, but the official 1.8 optimization has increased by 20% in terms of GC and Compile. it may be that this demo is too simple.

2. Python, which has been playing well recently


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

Next is Py 3.5.


qiangjian@localhost:/works/learnCPP$ python3 -V && time python3 ./fib.py Python 3.5.1real  0m3.110suser  0m2.982ssys   0m0.026s

At a glance, we can see that the biggest problem with Py is: the slower the upgrade, and the worse the problem is that many syntaxes are incompatible, but it is not bad to write algorithms and small programs at ordinary times. if it is other times, forget it, use Go.

3. PHP. I have used a lot of work, so I have to compare it.


 

Because I mainly use php5.4 in my work, the following is the first 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 the following problems occur:


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 GroupZend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologiesreal  0m2.307suser  0m2.278ssys   0m0.017s

PHP 7 is used for new projects and self-developed projects. please refer:


qiangjian@localhost:/works/learnCPP$ php -v && time php fib.phpPHP 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

I feel that php7 and 5 are quite different from each other. they are not a thing at all, and the progress has been greatly improved. I would like to likes it here! We recommend that you use php7.

4. C ++ is my favorite theoretical basis, of course C ++ 11/14, not old-fashioned c99.


#include 
  
    constexpr int fibonacci(const int i){  if(i<2) return i;  return fibonacci(i-2) + fibonacci(i-1);} int main() {  fibonacci(34);  return 0;}
  

First use g ++ 6.2 without optimization:


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

After optimization-O2 is added,


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 quite obvious, and the running time is only half of the former.

5. C also writes


#include 
  
    int fibonacci(int i){  if(i<2) return i;  return fibonacci(i-2) + fibonacci(i-1);}int main(){  printf("%d",fibonacci(34));}
  

Without 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

Addition-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

Like C ++ 14, the speed after optimization is significantly faster than doubled.

6. Java is the least I want to write. although it is very popular, it feels too bloated.


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);  }}

The result of compilation 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

The performance is good. The Compile time is too low compared with c ++/go.

7. The final appearance is of course the javascript that has always been very popular. no, it is actually Nodejs (this stuff has nothing to do with java tmd)


function fibonacci(i){  if(i<2) return i;  return fibonacci(i-2) + fibonacci(i-1);}console.log(fibonacci(34))

Result:


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.3 s, less than 0.5 s in total, almost close to java, but thanks to Google dad and Chromium dad's V8 Son and the open-source community for their advantages in code volume and maintainability.

If Nodejs runs stably, it is not allowed to unify the "program ". of course, I just want to talk about it. Don't be too serious.

Let's see the figure below:

Summary:

I feel that each language has different purposes, and its performance is just a single indicator. what I personally value is: readability, maintainability, portability, robustness, and scalability, followed by performance. In addition, modern hardware is becoming more and more powerful, and 8 GB of mobile phones are not moving. the cpu is catching up with the cpu of the PC five years ago, and ssd is becoming more and more popular .... I am optimistic about Golang, php, and python. I also pay attention to modern C ++, such as 14 and 17. as for rust, swift, java, and scala, this is mainly related to personal needs and the company's technology stack. Haha! Write so much first!

The above is a detailed analysis of the sample code for golang, python, php, c ++, c, java, and Nodejs performance comparison. For more information, see other related articles in the first PHP community!

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.