A Benchmark About Speed of Programming Languages

Yeah, yet another pointless benchmark about speed of programming languages.
Why I call benchmarks pointless is- The speed of a programming language varies widely with use cases.
Language X might be good at executing loops, while language Y might not be.

Additionally, you have to take into account the time spent in developing the programs.
Interpreted languages like PHP, Python, Ruby make a developer’s life very productive and easy, while it may not be for a C or C++ developer.

I am not a fanboy of any language, I just use the language which suits the requirements perfectly.

The languages I’m comparing in this benchmark are C, PHP, Python, Ruby.

The machine used for benchmark:

  • Gentoo Linux (x86_64) on Intel Dual Core 2 Ghz E2180 with 3 GB RAM
  • Since I use Gentoo Linux, everything that supports compiling using user specified CFLAGS and CXXFLAGS is compiled with the compiler flags -O3 -march=native -mtune=native.
  • So, the binaries is highly optimized specifically for E2180.

I have seen many people mocking Gentoo Users about these compiler flags, but I’ve seen some real improvements practically (as a server administrator, and as a daily Linux user), so they work for me. If they don’t work for you, boo, your bad luck.

C – The god

Quite as expected, the compiled C program had the lowest runtime and CPU usage.

The C program was compiled as follows:


So the time average is 0.002s and CPU average is 55%

The C code used for test:

PHP – The runner up

It was quite surprising me to me, that PHP came out to be the second in this test since PHP is well known for it’s sluggishness.
And here are the results for PHP 5.4.5:

So time average here is 0.1046s and CPU average is 97%

The PHP code used for test:

Ruby – Third

Ruby came out to be third in the test. I don’t know much of Ruby, have just started learning it using Ruby Koans

The results for Ruby 1.9.3p194 (2012-04-20 revision 35410):

Average time is 29.46s and CPU average is 98%

The code used for test:

There’s probably a better way to implement this, if you know, do comment

Python – Loser

Python was the loser in this test. I learned Python after PHP and it become my favorite language. It still is, but it slowly is being replaced by Ruby. Ruby’s syntax is just too good.

I used Python 3.2.3 and got this result:

Average time here is 61.36s, that’s slightly over a minute and CPU usage is 99%

The python code used to test:

There’s probably a better way of doing this, if you know, do comment


The conlcusion is pretty clear:

  • C is the fastest language
  • PHP is 52 times slower than C
  • Ruby is 14,730 times slower than C
  • Python is 30680 times slower than C

I don’t intend to prove that language X is better than Y or vice versa. It’s just a statistical benchmark and has almost no practical value.
There are just too many factors to be considered in production environment and most of the times, the database is the bottleneck.

If you’re concerned only about webapps, then PHP, Ruby and Python are best. You can build webapps in C/C++ as well using the fastcgi library, but it gets difficult after a point due to lack of libraries.
I have one such project called cxxcms where I attempted to build a CMS using C++ and the fastcgi library.

Where I got stuck: The template system.
I’m yet to find a proper template language for C++ with loops, conditionals, etc. Presently, I don’t have free time, but might build a lexer using pure C++ and Regex by hand, because flex/bison just goes over my head.

That’s all for now.

30 thoughts on “A Benchmark About Speed of Programming Languages

Add yours

  1. The reason that the c is so fast is that it’s not doing anything. Using -O3 will completely remove the loop since none of the variables are used.
    change return 0; to return a + b + c; and see what happens.

    1. I changed my hardware to i5-4670, so I can’t test it anymore, but I get what you’re saying.
      But still C will remain the fastest because in C every instruction is translated into assembly which directly tells you the speed of the CPU.
      In case of others like Ruby/Python, the statement is interpreted, so the effective speed you see is the speed of the CPU + speed of interpreter.

  2. For a fair comparison (keeping the variables in the 32 bit (?) integer domain) with Python you should do:

    #!/usr/bin/env python3
    max = 100000
    a = b = c = 0
    for i in range(max):
    a = (a + b + i) % 2**32
    b = (a * 5678 * i) % 2**32
    c = (i * i) % 2**32

  3. I think you should consider the precision of the results… for example in C the result is only 4 bytes and in php the result is almost immediately INF… however python gives a really really long number (not sure if it’s correct but I printed it to a file and it’s 822KiB …)… About Ruby I have no idea

  4. in your python code, use xrange instead of range, range will try to create an array with 100K elements in it. it will drop performance significantly.

    1. Me, coming from C, PHP and Python, I find it less readable.
      Is that going to make any real difference instead of the for loop I mentioned? Except that the for loop does one iteration more because I specified 1..max instead of 1…max

    2. Nope. Just syntax change wont do much good. Giving it some more logic might. Wait, will try that code of mine in Ruby and check.

    3. More readable than what @google-2ff62ce4120fba75903decc672dcd88b:disqus posted. But what’s the point? The for loop is fine, right? Unless there are some performance reasons.

    4. The for loop is just fine. Its not the way people do it in ‘Ruby’ community. Hardly see for loops in any code I have read.

    5. No. its not fine to write C like procedural code in Ruby. Instead, try to think in terms of objects. In Object Oriented land, Objects are expected to implement loop on their own & provide iterators. do explorer & playaround higher order functions like map,select,blocks & lambdas which shows the true power of iterators.

      In Ruby, for Loop is just syntactic sugar. when you writefor aSong in songList aSong.playend
      Ruby translates it into something like:
      songList.each do |aSong| aSong.playend
      The only difference between the for loop and the each form is the scope of local variables that are defined in the body
      So the point being,
      If you ever find yourself needing to use “for loop”, take a pause & rethink your implementation.

  5. Looping and simple arithmetic operations are hardly any benchmark. Let me know what tool you’re using to benchmark the scripts.

    1. I got the following results with those programs:
      Compiled the c code using gcc -O3 -march=native -mtune=native.

      4.110000 seconds
      9592 primes

      6.6309580802917 seconds
      9592 primes

      8.17414712906 seconds
      9592 primes

      And with Python 3.2.3 (just added brackets around print statements)

      10.068526983261108 seconds
      9592 primes


      Are you using PyPy or something similar? That’s not available for Python 3. For the code I made, the run times were same on Python 2 and 3 using CPython.

      Also, from the results of mine and yours, it seems Python is bad at computing math as compared to PHP. C of course would win in almost every case because the code is in binary format before it starts execution.

    2. That python code is not dependent on anything. Not even version. As you can see the print statements are out of timer. And in my case, Python performed good.
      An yes, C has the upper hand because of that fact. PHP lacks that capability of being run in binary format. Python can be compiled to binary and maybe can be run from that. Will have to check.

    3. Python can be compiled to a binary? Link?
      PHP can also be a compiled to C++ using Facebook’s hiphop, but that’s not a task everyone can do. I tried it and didn’t get it working.
      Otherwise there’s PHP apc which simply caches the compiled code in memory for reuse, there’s no pre-compiler (other than hiphop) to my knowledge.

    4. PHP is an interpreted language. It doesn’t get compiled while running. And Sorry, not binary, but Python codes are usually compiled to bytecode. You can call it binary format if you want(A lot of people do, it’s not though). Try pycompile command with -f (force rebuilt of byte-code) option.

    5. Ah, I knew about python bytecode. It just increases loading speed of the file.
      I don’t think I said PHP is not an interpreted language. It converts line by line to machine code.

    6. PHP is an interpreted language. It doesn’t get compiled while running. And Sorry, not binary, but Python codes are usually compiled to bytecode. You can call it binary format if you want(A lot of people do, it’s not though). Try pycompile command with -f (force rebuilt of byte-code) option.

    7. And, actually, running the program just once wont be much fair. Try running the code for say 5-10 times and get the average,min,max time taken.

    8. Exactly. I already stated in the post that this has almost no practical value.
      It would probably make more sense to implement some data structure and compare, or access and process rows out of an SQLite database.

      To benchmark, I used time command on shell.

      If you have some suggestions as to what practical problem should be implemted to test, please comment. I’ll try to do one such article.

Have something to add? Do it here.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑

%d bloggers like this: