why python is slower than java?

Brian Beck exogen at gmail.com
Sun Nov 7 15:34:12 CET 2004


Pythogoras wrote:
> OK. I will rewrite it in Java.
> 
> ///
> long t = currentTimeMillis();
> tryCopyFile("in.txt","out.txt");
> out.println( (currentTimeMillis() - t) / 1000);
> ///
> 
> You lost. The Java-version is shorter!
> I didn't count import- and import static-statements
> because Eclipse cares about imports automatically.

I don't know what version of Java they're using in this troll's fantasy 
land, but tryCopyFile does not, and has never existed in any Java 
implementation.  The real code for copying a file would look something 
like the contents of this function (which is not a part of the standard 
library):

public static void copy(File source, File dest) throws IOException {
      FileChannel in = null, out = null;
      try {
           in = new FileInputStream(source).getChannel();
           out = new FileOutputStream(dest).getChannel();

           long size = in.size();
           MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 
0, size);

           out.write(buf);

      } finally {
           if (in != null)          in.close();
           if (out != null)     out.close();
      }
}




More information about the Python-list mailing list