search
HomeBackend DevelopmentPHP TutorialDetailed analysis of sample codes for performance comparison of golang, python, php, c++, c, java, and Nodejs

This article mainly introduces the relevant information on the performance comparison of golang, python, php, c++, c, java, and Nodejs. Friends in need can refer to it

When I was working in PHP/C++/Go/Py, I suddenly had the idea to make a simple comparison of the performance of the recent mainstream programming languages ​​. As for how to compare, I still have to use the magical Fibonacci algorithm. Maybe it’s more commonly used or fun.

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

1. Why is Go the first one? Because I am using it personally recently and I feel very good.


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

Let’s take a look with 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/amd64
real    0m0.206suser    0m0.165ssys     0m0.059s
real    0m0.052suser    0m0.045ssys     0m0.004s

Then, take a look at 1.8:

The code is as follows:

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

It feels like There is no difference, but the official 1.8 has been optimized and improved by 20% in aspects such as GC and Compile. Maybe this demo is too simple.

2. Python has been very popular recently, so let’s compare it


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 first


qiangjian@localhost:/works/learnCPP$ python2 -V && time python2 ./fib.py 
Python 2.7.13
5702887

real 0m2.651s
user 0m2.594s
sys 0m0.027s

Then comes Py 3.5


qiangjian@localhost:/works/learnCPP$ python3 -V && time python3 ./fib.py 
Python 3.5.1

real  0m3.110s
user  0m2.982s
sys   0m0.026s

The biggest problem of Py can be seen at a glance: the more you upgrade, the slower it becomes, and the terrible thing is a lot of syntax It is not compatible, but it is good for writing algorithms and small programs. For other times, forget it and use Go.

3. PHP, I use it a lot for my work, so I have to compare it.


<?php
function fibonacci($i){
  if($i<2) return $i;
  return fibonacci($i-2) + fibonacci($i-1);
}
echo fibonacci(34);

Since I mainly use php5.4 for my work, so 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 Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
real  0m2.288s
user  0m2.248s
sys   0m0.021s

The test environment is 5.6, so the next wave is:


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

New projects, myself Everything you play is php7, please see:


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

I feel that php7 and 5 are completely different, not the same thing at all, and the progress has been too great, so I rely on this Thumbs up Brother Bird! I suggest you use php7 more.

4.C++ is my favorite theoretical basis. Of course, I am talking about C++11/14, not the old 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(34);
  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.378s
user  0m0.254s
sys   0m0.104s

real  0m0.050s
user  0m0.043s
sys   0m0.002s

After adding optimization -O2,


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

The effect is still very obvious, and the running time is only half of the former.

5. C also wrote


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

without optimization:


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

added -O2 optimization:


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

is the same as C++14. After optimization, the speed is significantly improved, twice as fast.

6.Java is what I least 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 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.952s
user  0m1.302s
sys   0m0.144s

real  0m0.150s
user  0m0.123s
sys   0m0.025s

The performance is okay, but the Compile time is too low compared to c++/go.

7. Of course, the last one to appear is javascript, which has always been popular. No, to be precise, it is Nodejs (this thing has nothing to do with java)


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.0

real  0m0.332s
user  0m0.161s
sys   0m0.062s

The result is still shocking. It only takes TMD 0.3s, and the total is less than 0.5s, almost It's close to Java, but the advantages of code volume and maintainability are really thanks to Google, Chromium's V8 son, and the open source community.

If Nodejs really runs stably, it may not really be able to unify the "program world". Of course, I'm just talking, don't take it too seriously.

Let’s take a picture:

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

##Summary:

I feel that each language has different uses, and performance is just a single indicator. I What I value more is: readability, maintainability, portability, robustness, scalability, and then performance. Moreover, modern hardware is becoming more and more powerful. Mobile phones often have 8G, CPUs have caught up with the CPUs of PCs 5 years ago, and SSDs are becoming more popular... I am more optimistic about Golang/php/python, and also pay attention to modern C++, such as 14 and 17. As for rust,

swift, java, scala, forget it, this mainly depends on personal needs, Related to the company’s technology stack. Ha ha! Let’s write this much first!

The above is the detailed content of Detailed analysis of sample codes for performance comparison of golang, python, php, c++, c, java, and Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.