Simple Speed Test Between a Bunch of Languages

by Gene Michael Stover

Sunday, 28 April 2002


I was learning Perl the other day. For lack of anything else to program initially, I write a few useless tiny programs. Since they didn't do anything useful, naturally I timed their execution. What's the point in knowing how long they required to run if I didn't know how long similar programs in other languages required to run? So I wrote them in Lisp, too. Then in C. Before I knew it, I had written speed tests in just about every language I see every day. (All except Bourne shell, I guess.)

So here, for you, are the results from one of those test programs.

The Test Programs

A test program computes a Fibonacci number many times. It uses the definitive, but naive, recursive algorithm. That's right; said algorithm is grossly inefficient, repeating calculations which don't need repeating. That makes it perfect for a CPU-bound performance test that doesn't require a lot of memory.

Some people will take joy in pointing out that a single test, especially such a simple one, cannot be representative of the performance of a whole language. Those people would be right. Those people would also be annoying, so if you are such a person, just read the numbers, think about how naive I am, & keep your opinions to yourself, or at least away from me.

Test Conditions

I ran these tests on palsy.CyberTiggyr.COM. It's an Intel Pentium whatever with 128 megabytes of real memory, running OpenBSD 2.7.

C & C++ programs were compiled with Gnu's gcc version 3.0.4. The Java compiler & run-time were from JDK 1.3.1 for Linux, which I obtained from Sun's Java site. Lisp was clisp version 2.28. Perl was version 5.6.1. Only the good Lord above, or the bad one below, knows that version of Python I used.

No other users were logged in, & I wasn't running any other programs at the time.

The Results

Here are some results. (All in separate files of their own.)

iterations F in "(fib f)" report
1 20 results-0001-20.html
1 25 results-0001-25.html
1 30 results-0001-30.html
1 35 results-0001-35.html
1 37 results-0001-37.html
1 40 results-0001-40.html

You might notice that fibb, the Bourne shell program, has a showing only in tests with low F. That's because it's a couple orders of magnitude slower than the others.

You can create your own results, too. The program fib-go runs all the tests & puts their results in an HTML file which it prints to STDOUT. Just run fib-go, redirect the output to your own file, & load that file into your Web browser. Control the number of iterations with a required I command line argument & with a required F argument. For example, "fib-go 10 20" would run 10 iterations of fibonacci(20) for each language.

You can download the whole kit-&-kaboodle containing this article, my files of results, & the programs, from my page at Freeshell. That's speed-fib.tar.bz2.


C & C++ are effectively the same. This test didn't use any special or object-oriented features of C++, though.

Java differs from C/C++ by a surprisingly small amount. It looks like the cost of Java is about 1.4 that of C, so it's the same order of magnitude as C. That's without any object-oriented features of Java, though.

Lisp is two orders of magnitude slower than C/C++.

Python & Perl are nearly three orders of magnitude slower than C/C++.