?

Log in

Recursive benchmark with D/C++ - leonardo
View:Recent Entries.
View:Archive.
View:Friends.
View:Profile.
View:Website (My Website).

Tags:, , ,
Security:
Subject:Recursive benchmark with D/C++
Time:05:58 pm
I have drilled down on one of the benchmarks done last time. Here the C++ and D code are nearly the same, to better see compiler and compilation differences. The results aren't good for D, and taking a look at the asm produced, llvm-g++ seems able to unroll the recursive struct method much more than the ldc compiler (that uses the same backend).
Timings, N = 35:
  fib_mod_d:   5.83 s (ldc)
  fib_mod_c++: 4.82 s (llvm-g++)
  fib_mod_c++: 3.82 s (g++)

--------------------

BENCHMARK DETAILS:

CPU: Pentium3 500 Mhz, 256 MB RAM
OS: Ubuntu 8.10

Timings done with the 'time' command, 
 'real' timings, best of 3 runs.

Code compiled with:
  ldc -O3 -release -inline fib_mod_d.d
  g++ -O3 -s fib_mod_cpp.cpp -o fib_mod_cpp
  llvm-g++ -O3 -s fib_mod_cpp.cpp -o fib_mod_cpp

Compilers used:
  LLVM D Compiler rev.939, based on DMD v1.039
   and llvm 2.4 (Wed Feb  4 23:09:12 2009)
  
  gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12) 

--------------------- 

CODE:

// C++ version
#include "stdio.h"

struct Fib {
    int _value;
    Fib(int n) { _value = n; }

    int value() {
        if (_value <= 2)
            return 1;

        Fib f1 = Fib(_value - 1);
        Fib f2 = Fib(_value - 2);

        return f1.value() + f2.value();
    }
};

int main() {
    int tot = 0;
	for (int i = 0; i < 10; i++) {
		Fib x = Fib(35);
		tot += x.value();
	}
    printf("tot: %d\n", tot);
	return 0;
}

--------------------------

// D version
import tango.stdc.stdio: printf;

struct Fib {
    int _value;

    int value() {
        if (_value <= 2)
            return 1;

        Fib f1 = Fib(_value - 1);
        Fib f2 = Fib(_value - 2);

        return f1.value() + f2.value();
    }
};

int main() {
    int tot = 0;
	for (int i = 0; i < 10; i++) {
		Fib x = Fib(35);
		tot += x.value();
	}
    printf("tot: %d\n", tot);
	return 0;
}

--------------------------

"Cleaned up" asm of D code produced by LDC:

_D9fib_mod_d3Fib5valueMFZi:
	pushl	%esi
	subl	$16, %esp
	movl	(%eax), %eax
	cmpl	$2, %eax
	jg	.LBB2_3	# endif
.LBB2_1:	# if
	movl	$1, %eax
.LBB2_2:	# if
	addl	$16, %esp
	popl	%esi
	ret
.LBB2_3:	# endif
	leal	-1(%eax), %ecx
	movl	%ecx, 8(%esp)
	addl	$4294967294, %eax
	movl	%eax, (%esp)
	leal	8(%esp), %eax
	call	_D9fib_mod_d3Fib5valueMFZi
	movl	%eax, %esi
	leal	(%esp), %eax
	call	_D9fib_mod_d3Fib5valueMFZi
	addl	%esi, %eax
	jmp	.LBB2_2	# if

_Dmain:
.Leh_func_begin1:
.Llabel1:
	pushl	%ebx
	pushl	%edi
	pushl	%esi
	subl	$24, %esp
	xorl	%esi, %esi
	movl	%esi, %edi
	.align	16
.LBB1_1:	# forbody
	movl	$34, 16(%esp)
	movl	$33, 8(%esp)
	leal	16(%esp), %eax
	call	_D9fib_mod_d3Fib5valueMFZi
	movl	%eax, %ebx
	leal	8(%esp), %eax
	call	_D9fib_mod_d3Fib5valueMFZi
	addl	%edi, %ebx
	addl	%eax, %ebx
	incl	%esi
	cmpl	$10, %esi
	movl	%ebx, %edi
	jne	.LBB1_1	# forbody
.LBB1_2:	# endfor
	movl	%ebx, 4(%esp)
	movl	$.str1, (%esp)
	call	printf
comments: Leave a comment Previous Entry Share Next Entry

(Anonymous)
Link:(Link)
Time:2009-02-12 10:50 pm (UTC)
You're comparing a production-ready implementation of C++ with a not yet production-ready implementation of D. Makes it kinda bogus, doesn't it?

But I'm sure the LDC developers value your input on that topic.

Kind regards,
Alexander Pánek aka JimPanic
http://panic.brainsware.at/
(Reply) (Thread)

Recursive benchmark with D/C++ - leonardo
View:Recent Entries.
View:Archive.
View:Friends.
View:Profile.
View:Website (My Website).