why python is slower than java?

Alex Martelli aleaxit at yahoo.com
Sat Nov 6 12:10:47 CET 2004


On 2004 Nov 06, at 08:31, Maurice LING wrote:
    ...
> 1. it is a disk intensive I/O operation.
> 2. users delay is not in the equation (there is no user input)
> 3. I am not interested in the amount of time needed to develop it. But 
> only interested execution speed.

OK, could you provide a simple toy example that meets these conditions 
-- does lot of identical disk-intensive I/O "in batch" -- and the 
execution speed measured (and on what platform) for what Python and 
Java implementations, please?

For example, taking a trivial Copy.java from somewhere on the net:

import java.io.*;

public class Copy {
     public static void main(String[] args) throws IOException {
         File inputFile = new File("/usr/share/dict/web2");
         File outputFile = new File("/tmp/acopy");

         FileReader in = new FileReader(inputFile);
         FileWriter out = new FileWriter(outputFile);
         int c;

         while ((c = in.read()) != -1)
            out.write(c);

         in.close();
         out.close();
     }
}

and I observe (on an iBook 800, MacOSX 10.3.5):

kallisti:~ alex$ java -version
java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-141.3)
Java HotSpot(TM) Client VM (build 1.4.2-38, mixed mode)

-r--r--r--  1 root  wheel  2486825 12 Sep  2003 /usr/share/dict/web2

kallisti:~ alex$ time java Copy

real    0m7.058s
user    0m5.820s
sys     0m0.390s

versus:

kallisti:~ alex$ time python2.4 Copy.py

real    0m0.296s
user    0m0.080s
sys     0m0.170s

with Python 2.4 beta 1 for the roughly equivalent:

inputFile = file("/usr/share/dict/web2", 'r')
outputFile = file("/tmp/acopy", 'w')

outputFile.write(inputFile.read())

inputFile.close()
outputFile.close()

which isn't all that far from highly optimized system commands:

kallisti:~ alex$ time cp /usr/share/dict/web2 /tmp/acopy

real    0m0.167s
user    0m0.000s
sys     0m0.040s

kallisti:~ alex$ time cat /usr/share/dict/web2 >/tmp/acopy

real    0m0.149s
user    0m0.000s
sys     0m0.090s


I'm sure the Java version can be optimized easily, too -- I just 
grabbed the first thing I saw off the net.  But surely this example 
doesn't point to any big performance issue with Python disk I/O wrt 
Java.  So, unless you post concrete examples yourself, the smallest the 
better, it's going to be pretty difficult to understand where your 
doubts are coming from!


Alex




More information about the Python-list mailing list