Why Java (server VM) is faster than D?

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 3 09:40:38 PDT 2015


On Monday, 3 August 2015 at 16:27:39 UTC, aki wrote:
> When I was trying to port some Java program to D,
> I noticed Java is faster than D.
> I made a simple bench mark test as follows.
> Then, I was shocked with the result.
>
> test results on Win8 64bit (smaller is better)
> Java(1.8.0,64bit,server): 0.677
> C++(MS vs2013): 2.141
> C#(MS vs2013): 2.220
> D(DMD 2.067.1): 2.448
> D(GDC 4.9.2/2.066): 2.481
> Java(1.8.0,32bit,client): 3.060
>
> Does anyone know the magic of Java?
>
> Thanks, Aki.
>
> ---
>
> test program for D lang:
> import std.datetime;
> import std.stdio;
> class Foo {
> 	int i = 0;
> 	void bar() {}
> };
> class SubFoo : Foo {
> 	override void bar() {
> 		i = i * 3 + 1;
> 	}
> };
> int test(Foo obj, int repeat) {
> 	for (int r = 0; r<repeat; ++r) {
> 		obj.bar();
> 	}
> 	return obj.i;
> }
> void main() {
> 	auto stime = Clock.currTime();
> 	int repeat = 1000 * 1000 * 1000;
> 	int ret = test(new SubFoo(), repeat);
> 	double time = (Clock.currTime() - stime).total!"msecs" / 
> 1000.0;
> 	writefln("time=%5.3f, ret=%d", time, ret);
> }
>
> test program for Java:
> class Foo {
> 	public int i = 0;
> 	public void bar() {}
> };
> class SubFoo extends Foo {
> 	public void bar() {
> 		i = i * 3 + 1;
> 	}
> };
> public class Main {
> 	public static int test(Foo obj, int repeat) {
> 		for (int r = 0; r<repeat; ++r) {
> 			obj.bar();
> 		}
> 		return obj.i;
> 	}
> 	public static void main(String[] args) {
> 		long stime = System.currentTimeMillis();
> 		int repeat = 1000 * 1000 * 1000;
> 		int ret = test(new SubFoo(), repeat);
> 		double time = (System.currentTimeMillis() - stime) / 1000.0;
> 		System.out.printf("time=%5.3f, ret=%d", time, ret);
> 	}
> }

Not surprising. The virtual function call takes almost all of the 
time and the JVM will be devirtualising it. If you want to call 
tiny virtual functions in tight loops, use a VM.

That said, it's a bit disappointing that the devirtualisation 
doesn't happen at compile-time after inlining for a simple case 
like this.


More information about the Digitalmars-d mailing list