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 + ' '
        else:
            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)+' ')

f.close()
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:
            mArray.append(float(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)
    {
        try
        {
            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);
            }
            out.close();
            long t2 = System.nanoTime();
            Scanner s = new Scanner(new BufferedInputStream(new FileInputStream(f)));
            s.useLocale(Locale.US);
            double[] array = new double[1000000];
            int k = 0;
            while(s.hasNextDouble())
            {
                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)
        {
            ex.printStackTrace();
        }
    }
}

The result

Python

Time to write: 1.46744799614
Time to read: 1.57424807549

Java

Time to write: 4.46359321
Time to read: 8.921353764

#java, #python