Home > Uncategorized > Language benchmark: Java vs Python

Language benchmark: Java vs Python

Here is a nice web site to compare languages properties: http://shootout.alioth.debian.org. But I did not found the test I was looking for: writing/reading float text stream. So I wrote mine.

The python code

Note that python don’t have function to tokenize a stream so we need the ugly readword function:

def readword(st, bufferSize=4096):
    """Tokenize a stream into word. Words are expected to be separated by one
    or more spaces."""
    buffer = st.read(bufferSize)
    readSize = len(buffer)
    #read as many as 'bufferSize' blocks to reach the end of the stream
    while buffer:
        l = buffer.split(' ');
        #is it the last block ?
        if readSize == bufferSize:
            # properly handle the cut between to blocks. The last word of the
            # current block while be handled by the next interation.
            endOfBuffer = l.pop();
            # if the blocks end with "XX " we added only "XX", it must be
            # fixed
            if buffer[len(buffer)-1].isspace():
                endOfBuffer = endOfBuffer + ' '
            endOfBuffer = ''
        for word in l:
            if word:
                yield word
        buffer = st.read(bufferSize)
        readSize = len(buffer)
        buffer = endOfBuffer + buffer

import time
import array
import tempfile
import os

t1 = time.time()

fd, name = tempfile.mkstemp()
f = os.fdopen(fd, "w")

for i in xrange(1000000):
    f.write(str(i)+' ')

t2 = time.time()

f = open(name)
for i in xrange(1):
    f = open(name)
    mArray = array.array('f')
    for s in readword(f):
        if s:

t3 = time.time()
print "Time to write: " + str(t2-t1)
print "Time to read: " + str(t3-t2)

The Java code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.PrintStream;
import java.util.Locale;
import java.util.Scanner;

public class JavaVsPython
    public static void main(String[] args)
            long t1 = System.nanoTime();
            File f = File.createTempFile("xxx", "");
            PrintStream out = new PrintStream(new FileOutputStream(f));
            for(int i=0; i<1000000; i++)
                out.print(" "+(double)i);
            long t2 = System.nanoTime();
            Scanner s = new Scanner(new BufferedInputStream(new FileInputStream(f)));
            double[] array = new double[1000000];
            int k = 0;
                array[k++] = s.nextDouble();
            long t3 = System.nanoTime();
            System.out.println("Time to write: "+(t2-t1)/1E9);
            System.out.println("Time to read: "+(t3-t2)/1E9);
        catch (Exception ex)

The result


Time to write: 1.46744799614
Time to read: 1.57424807549


Time to write: 4.46359321
Time to read: 8.921353764
Tags: ,
  1. No comments yet.
  1. May 28, 2010 at 18:39

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: