Enthusiasm never stops

C++ vs. Python vs. Perl vs. PHP performance benchmark (part #2)


This time we will focus on the startup time. The process start time is important if your processes are not persistent. If you are using FastCGI, mod_perl, mod_php, or mod_python, then these statistics are not so important to you. However, if you are spawning many processes which do something small and live for a very short time, then you should consider the CPU resources which get wasted while the script interpreter is being initialized.

The benchmarked scripts do only one thing – say “Hello, world” on the standard output. They do not include any additional modules in their source code – this may, or may not be your use-case. Though, very often the scripting languages have pretty many built-in functions, and for simple tasks you never need to include other modules.

Here are the benchmark results:

Language CPU time Slower than
User System Total C++ previous
C++ (with or w/o optimization) 2.568 3.536 6.051
Perl 12.561 6.096 18.723 209% 209%
PHP (w/o php.ini) 20.473 13.877 34.918 477% 86%
Python 27.014 11.881 39.318 550% 13%
Python + Psyco 32.986 14.845 48.132 695% 22%

The clear winner among the script languages this time is… Perl. 🙂

All scripts were invoked 3000 times using the following Bash loop:

time ( i=3000 ; while [ “$i” -gt 0 ]; do $CMD >/dev/null ; i=$(($i-1)); done )

All tests were done on a Kubuntu Lucid box. The versions of the used software packages follow:

  • g++ (GNU project C and C++ compiler) 4.4.3
  • Python 2.6.5
  • Python Psyco 1.6 (1ubuntu2)
  • Perl 5.10.1
  • PHP 5.3.2 (1ubuntu4.2 with Suhosin-Patch), Zend Engine 2.3.0

The C++ implementation follows, click “show source” below to see the full source:

#include <iostream>
using namespace std;

int main() {
	cout << "Hello, world!\n";
	return 0;

The Perl implementation follows, click “show source” below to see the full source:

use strict;
use warnings;

print "Hello, world!\n";

The PHP implementation follows, click “show source” below to see the full source:

echo "Hello, world!\n";

The Python implementation follows, click “show source” below to see the full source:

#import psyco

print 'Hello, world!'

Update (Jan/14/2012): Copied the used test environment info here.


Author: Ivan Zahariev

An experienced Linux & IT enthusiast, Engineer by heart, Systems architect & developer.

6 thoughts on “C++ vs. Python vs. Perl vs. PHP performance benchmark (part #2)

  1. With GCC 4.1.2 and GLIBC 2.9, kernel 2.6.30:
    Launching a “hello world” program 5000 times (output to /dev/null) on a 2.67Ghz Pentium IV:

    Perl 5.8.8: 15.4 seconds
    C++ dynamically linked executable: 11.3 seconds
    C++ statically linked executable: 1.13 second
    C dynamically linked executable: 2.2 seconds
    C statically linked executable: 0.64 second
    C statically linked with dietlibc: 0.25 second

    1) Dynamically linking is slow to start up.
    2) C++ contains much startup overhead.
    3) kernel’s fork/exec overhead is very low for simple programs.
    4) Dietlibc start up is very fast.

  2. Hello,
    My name is Nadav and I am a PhD candidate researching compiler optimizations. I like your comparison because it gives a pretty good estimate as to what overhead we see when benchmarking applications. It would be interesting to look at the call graph and see where time is spent.

    • Hey, I’m glad that you could use this in your research.

      The calls in Linux are divided in two main parts – user-space and kernel-space (system) calls. The system calls are much less as a count, and you could do some sort of comparison there. I suppose that the “high-level” languages accumulate system-calls CPU time by making too many memory operations; but that’s just shooting in the dark.
      The user-land calls are way too many and I’m not very optimistic, that you can summarize and compare the results.

      If you find out something interesting, please post it here, or give us a link, so that other people can read about it too.

    • I realize this is ~5 years old but I ran strace and here are the results:

      hopefully I’ll be able to make a useful graph out of it later tonight.

  3. Pingback: Python’s Flying Circus épisode 1: Python vs. Perl | Sacré Gr@@l

  4. Pingback: C++ vs. Python vs. Perl vs. PHP performance benchmark « /contrib/famzah

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s