I’m new in D and I’m comparing it vs Java in simple tests and expecting to see that the native language will be faster (or roughly the same). But it in my first test with recursion D is surprisingly slower than Java (almost two times).
Java (this is bad java perfomance test but it just simple idea):
public static void main(String... args) {
long before = System.nanoTime();
System.out.println(fibonacci(40));
System.out.println(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - before));
}
static int fibonacci(int n) {
if (n < 2) {
return n;
}
return fibonacci(n - 2) + fibonacci(n - 1);
}
Environment: Win7 64bit, JDK: 1.7.0_10 x64.
D:
import std.stdio;
import std.datetime;
void main(string[] args)
{
auto r = benchmark!(simplebench)(1);
writefln("%s", r[0].to!("msecs", int));
}
void simplebench() {
writeln(fibonacci(40));
}
int fibonacci(int n) {
if (n < 2) {
return n;
}
return fibonacci(n - 2) + fibonacci(n - 1);
}
Environment: Win7 64bit, dmd 2.061, compiler options: -noboundscheck -inline -O -release
Java ~570ms and D ~1011ms.
What am I doing wrong?
Thanks!
Java is also native via its JIT compiler. If you disable the JIT using
-Xint(forces interpreter) then you’ll see that D is significantly faster. For what it’s worth, I tried a similar implementation in C and got the same speed as in D.These sorts of micro benchmarks are not useful for testing general performance. All you are doing here is testing Java JIT compiled code versus D compiled code. They’re both being compiled. Also, this is not typical Java code. Typical Java programs allocates a lot of memory on the heap, whereas typical D programs do not.
If you want to learn about real performance of D versus Java then you need you test it on real programs.