Wednesday, August 16, 2006

Operator overloading in Java/Jython

Yesterday, I discovered that it is possible to add operator overloading to Java through Jython.

I created a small Point.java class:
import java.lang.String;
public class Point {
private double x, y;

public Point(double x, double y){
this.x = x; this.y = y;
}

public Point(Point other){
this.x = other.x; this.y = other.y;
}

public Point __add__(Point b) {
return new Point(this.x + b.x, this.y + b.y);
}

public String toString(){
String str = "(" + this.x + "," + this.y + ")";
return str;
}

public String __repr__(){
String str = "Point: (" + this.x + "," + this.y + ")";
return str;
}
}
I compiled this class with: javac Point.java. In the same folder I created a testPoint.py file with:
import Point

a = Point(1.0,1.0)
b = Point(2.0,2.5)
print "a = ", a
print "b = ", b
c = a + b
print "a + b = ", c
Running: jython testPoint.py, produces
a = (1.0,1.0)
b = (2.0,2.5)
a + b = (3.0,3.5)

Monday, August 14, 2006

Language performance

Since long time I wanted to answer a simple question: what is the difference in performance of different programming languages when solving numerical problems? I've read a lot about the subject on the web. Unfortunately, I found many different opinions.

Some people say Java is too slow for number crunching, others say it can be faster than plain C for some problems. Finally, I found some posts that claim that C# in .Net is faster than Java. Since I'm thinking to rewrite part of one of my codes I thought it would be a good idea to answer this question before starting my new project.

As first step, I decided to use a single benchmark focused on floating point operations. In Java, Scimark2 is the standard. There are also version for the other two languages (C and C#) that I used in my comparison.

After using Linux as my main desktop for the last 4 years, I moved back to Windows XP some weeks ago. That's why I also wanted to check the effect of the OS in the final performance. Finally, I wanted to check a wide range of C compilers, Java VMs, and .Net implementations (well, I only know two...).

TODO: Add results...
Scimark2 results in Windows XP Home SP2



Scimark2 results in Linux Fedora 5