Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 18, 2003 Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
You know, from the shootout. It's almost exactly twice as slow as the C version. I'm just curious more than anything else. Is this due to a tail-call optimization made by the C compilers? As a side note, is there any way to make dmd output the assembly language for the file you are compiling? (like the -S option in GCC) This would allow me see why it's slower. -- // DDevil The code: import string; int Ack(int M, int N) { if (M == 0) return( N + 1 ); if (N == 0) return( Ack(M - 1, 1) ); return( Ack(M - 1, Ack(M, (N - 1))) ); } int main(char[][] args) { int n = args.length < 2 ? 1 : string.atoi(args[1]); printf("Ack(3,%d): %d\n", n, Ack(3, n)); return(0); } |
March 18, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DDevil | "DDevil" <ddevil@functionalfuture.com> wrote in message news:pan.2003.03.18.12.37.59.960375@functionalfuture.com... > You know, from the shootout. It's almost exactly twice as slow as the C version. I'm just curious more than anything else. Is this due to a tail-call optimization made by the C compilers? Make sure you're compiling with -O -release. > As a side note, is there any way to make dmd output the assembly language for the file you are compiling? (like the -S option in GCC) This would allow me see why it's slower. Sure. Compile normally, then run OBJ2ASM on the .obj file output. |
March 18, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Tue, 18 Mar 2003 08:24:00 -0800, Walter wrote: > Make sure you're compiling with -O -release. Yep. Without -O it's twice slower again (ie. 4 times slower than C). -release makes no difference. I'm using the 0.59 compiler by the way. > Sure. Compile normally, then run OBJ2ASM on the .obj file output. Where can I get OBJ2ASM? Does dmd not use an intermediate assembly stage? Thanks -- // DDevil |
March 18, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DDevil | "DDevil" <ddevil@functionalfuture.com> wrote in news:pan.2003.03.18.17.16.12.727107@functionalfuture.com: > On Tue, 18 Mar 2003 08:24:00 -0800, Walter wrote: >> Make sure you're compiling with -O -release. > > Yep. Without -O it's twice slower again (ie. 4 times slower than C). -release makes no difference. I'm using the 0.59 compiler by the way. Huh. With -O DMD is twice as fast for me. Using the 0.57 compiler. |
March 18, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel Lucsy | On Tue, 18 Mar 2003 19:17:31 +0000, Joel Lucsy wrote:
> Huh. With -O DMD is twice as fast for me. Using the 0.57 compiler.
Hehe, right, that's what I said.
So without -O it's 4 times slower than C and with -O it's only 2 times slower. ;)
--
// DDevil
|
March 18, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Tue, 18 Mar 2003 08:24:00 -0800, Walter wrote:
> Sure. Compile normally, then run OBJ2ASM on the .obj file output.
OK, after looking at the assembly, it looks like D is not doing tail-call optimizing at all. GCC and MSVC optimize it very nicely and that's why they run so fast. They completely remove the recursive calls by using jmp. This effectively turns it into a for-loop.
It's interesting because I was comparing the Fibonacci Numbers benchmark which runs about the same in C and D. After looking at the assembly, I see that GCC is not able do tail-call optimizing for that benchmark (in theory it _should_ be able to, but it might be more difficult).
I can't remember when GCC does the tail-call optimizing so I don't know if using it as a back-end would help.
I believe tail-call optimizing would be a great feature in D because it would allow a more functional programming style when it makes sense. Not only would you gain speed, but you don't have to worry about blowing out the stack. That's a major beef I have with C and C++.
So my question is now: Does D ever do any tail-call optimizing?
--
// DDevil
|
March 18, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DDevil | DDevil wrote: > So my question is now: Does D ever do any tail-call optimizing? D does! But maybe DMD doesn't....Walter? -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
March 19, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DDevil | "DDevil" <ddevil@functionalfuture.com> wrote in message news:pan.2003.03.18.21.13.04.656244@functionalfuture.com... > I believe tail-call optimizing would be a great feature in D because it would allow a more functional programming style when it makes sense. Not only would you gain speed, but you don't have to worry about blowing out the stack. That's a major beef I have with C and C++. > > So my question is now: Does D ever do any tail-call optimizing? DMD currently does not do any tail recursion optimization. However, that is not a limitation of D, it is just the way the DMD compiler is implemented. There's nothing about D that would stop a D compiler from doing such optimizations. |
March 19, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Tue, 18 Mar 2003 16:01:08 -0800, Walter wrote:
> DMD currently does not do any tail recursion optimization. However, that is not a limitation of D, it is just the way the DMD compiler is implemented. There's nothing about D that would stop a D compiler from doing such optimizations.
OK, right, I should have phrased that as "does the DMD compiler currently do tail-call optimization". I wish more imperative language compilers supported such optimizations.
Thanks
--
// DDevil
|
March 19, 2003 Re: Why is Ackermann's function so much slower in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to DDevil | So....will the DMD compiler do tail-call optimizations? It that a compiler feature that's on the drawing board, or is it not a priority?
--Benji Smith
>On Tue, 18 Mar 2003 16:01:08 -0800, Walter wrote:
>> DMD currently does not do any tail recursion optimization. However, that is not a limitation of D, it is just the way the DMD compiler is implemented. There's nothing about D that would stop a D compiler from doing such optimizations.
>
>OK, right, I should have phrased that as "does the DMD compiler currently do tail-call optimization". I wish more imperative language compilers supported such optimizations.
|
Copyright © 1999-2021 by the D Language Foundation