Python vs. Java

Leif Johnson — 08 Mar 2003, 17:03

I ran across [Glyph Lefkowitz’ Python and Java tests][] while doing some research on integrating Python into non-scripting languages. Since the versions Glyph used were a little old, I became curious about how my current versions would pan out.

Note: As a coder who's worked a bit in both languages, I'm heavily biased towards Python !

Runtime performance

My test platform was an IBM laptop with a 500 MHz Pentium III processor and 192 MB of memory, running Debian GNU/Linux, kernel version 2.4.18. I used Python version 2.2.2 (installed from the Debian package) and the Java2 SDK version 1.4.1 (installed from Sun's web site, not from a Debian package).

Test Java Python P-J P/J
ConsoleTest 23.35 18.01 -5.34 0.7713
HashTest 2.08 3.96 1.88 1.90
IOTest 5.13 23.26 18.13 4.53
ListTest 1.48 5.57 4.09 3.76
NativeTest 2.43 3.98 1.55 1.64
NoTest 1.01 0.07 -0.94 0.07
ObjectTest 2.42 110.02 107.60 45.5
SpeedTest 0.94 1.31 0.37 1.4

A few notes about the table :

  • I ran my tests from behind an HTTP proxy, so I left out the URLTest, because I didn't have time to figure out how to deal with proxies.

  • The numbers in the third column display the absolute time difference between the two tests ; negative numbers indicate tests where Python was faster than Java.

  • The numbers in the fourth column indicate the relative speed difference between the two tests ; numbers smaller than 1 indicate a test where Python was faster.

  • All numbers reflect the number of significant figures returned by the time function, I hope.

Although there is no explicit license statement on Glyph's code, I assume it is distributed in the sense of the Python License or something similar, so I have [repackaged his code][], with my small modifications for updated versions.

Code size

One thing that Glyph comments on but does not quantify is the amount of code required for each of the tests. It is striking, from looking at his site, how small the Python code example is when compared with the Java code. I decided to try to quantify this, as I believe it is a significant factor when writing larger programs. All file sizes are measured in bytes and reflect source code size only. (Compiled code is not particularly a fair comparison, I think, since Java is byte-compiled and Python can be byte-compiled.)

Test Java Python P-J P/J
ConsoleTest 143 35 -108 0.24
HashTest 284 69 -215 0.24
IOTest 368 76 -292 0.21
ListTest 413 101 -312 0.245
NativeTest a 261 57 -204 0.22
NativeTest b 172 (563) 382 210 (-181) 2.22 (0.679)
NoTest 66 0 -66 0.0
ObjectTest 271 134 -137 0.494
SpeedTest 110 32 -78 0.29

A few notes about this table :

  • The NativeTest is composed of (a) test code written in Java or Python and (b) native code written in C. I decided it would be more illustrative to split the two parts and compare them separately.

  • Writing native code for Java involves writing a C file by hand and then using the javah program to automatically generate a C header file needed for compiling. The numbers in parentheses reflect the code sizes for Java that include this automatically generated file. This header file does not need to be written by hand, but it does increases the size of the code involved.

  • Python code, for these tests, is fairly consistently one fourth the size of Java code. I find this interesting and would like to see some more real–life code comparisons to see if such a strong trend holds.

  • None of the code for these tests, except for the generated native header file for Java's NativeTest, includes any comments.

More information

The Great Computer Language Shootout