Home
> Uncategorized > Language Benchmark: Java Vs Python (The return)

## Language Benchmark: Java Vs Python (The return)

In my previous post I compared I/O and string formating and python was better than Java. Anyway as Java have primitive types and do not need to box/unbox variables it could be realy faster than python. Here is an exemple where I compute the distances between 3D points.

### The python code

import time from numpy import * from math import * n = 1000000 v1 = zeros(n*3) v2 = zeros(n*3) dn = double(n) for i in xrange(n): v1[i] = 1.0 + i / dn; v2[i] = 2.0 + i / dn; r = zeros(n) t1 = time.time() for i in xrange(n/3): for j in xrange(3): r[i]=r[i] + (v1[i*3+j]-v2[i*3+j])**2 r[i]=sqrt(r[i]) t2 = time.time() print t2-t1

### The Java code

public static void main(String[] argv) { int n = 1000000; double[] v1 = new double[n*3]; double[] v2 = new double[n*3]; for(int i = 0; i < n; i++) { v1[i] = 1.0+i/(double)n; v2[i] = 2.0+i/(double)n; } double[] r = new double[n]; double tmp; long t1 = System.nanoTime(); for(int i = 0; i < n/3; i++) { for(int j = 0; j < 3; j++) { tmp = v1[i*3+j]-v2[i*3+j]; tmp = tmp*tmp; r[i] += tmp; } r[i] = Math.sqrt(r[i]); } long t2 = System.nanoTime(); System.out.println("time: "+(t2-t1)/1E9); for(int i = 0; i<n; i++) { System.out.println(r[i]); } }

### Results

- Java: 0.02s (with a precision of 0.05s)
- Python: 9.49s

### Conclusion

There are probably ways to improve my python code using numpy a better way, but most of the time to get rid of boxing/unboxing time you will have to write C code to speedup most critical part of algorithms. While Python is only a gluing (yet very good) technology, Java can be used to write fast low level algorithms.

Advertisements

JDK1.6.0_26’s 64-bit java -server executes that on my machine in 0.011s

In python, you’re often best off expressing things at as high a level as you can.. After writing the Python calculation as a single expression rather than iterating through each part of the each point manually…

import time

from numpy import *

n = 1000000

num_points = int(n / 3)

v1 = (arange(num_points * 3.).reshape((num_points, 3)) + 1) / n

v2 = v1 + 1

t1 = time.time()

r = sqrt(sum((v1 – v2) ** 2, axis=1))

t2 = time.time()

print t2-t1

… my machine with 32-bit Python 2.7 + Numpy 1.6 MKL executes that in 0.024s. Sorry I don’t have the 64-bit copy installed.

I didn’t do anything special there.. I assume there’s some way to move the allocation out of the timed loop and compile out the intermediates. Perhaps numexpr.