tail recursive delegates
kzed at port70.net
kzed at port70.net
Tue Jul 15 10:22:31 PDT 2008
Hello,
so I wanted to test whether tail recursion optimization worked with
delegates... And apparently I'm doing something wrong :) (Simple tail
recursion with functions worked, as in gcc.)
I'm using the GNU compiler; -O2 turns on tail recursion opt as with
gcc.
Can someone help?
I'll paste my test procedure.
Thanks,
Mate
test.d:
----
int delegate(int, int) counter(int by)
{
int cntr(int i, int acc) {
printf("cntr i=%d acc=%d by=%d\n", i, acc, by);
if(i < 1) return acc;
return cntr(i - 1, acc += by);
}
return &cntr;
}
int main()
{
int delegate(int, int) count = counter(3);
printf("%d", count(10, 0));
return 0;
}
---
eris ~% gdc -o test test.d
eris ~% ./test
cntr i=10 acc=0 by=3
cntr i=9 acc=3 by=3
cntr i=8 acc=6 by=3
cntr i=7 acc=9 by=3
cntr i=6 acc=12 by=3
cntr i=5 acc=15 by=3
cntr i=4 acc=18 by=3
cntr i=3 acc=21 by=3
cntr i=2 acc=24 by=3
cntr i=1 acc=27 by=3
cntr i=0 acc=30 by=3
30
eris ~% gdc -o test test.d -O2
eris ~% ./test
cntr i=10 acc=0 by=6530176
cntr i=9 acc=6530176 by=6530176
cntr i=8 acc=13060352 by=6530176
cntr i=7 acc=19590528 by=6530176
cntr i=6 acc=26120704 by=6530176
cntr i=5 acc=32650880 by=6530176
cntr i=4 acc=39181056 by=6530176
cntr i=3 acc=45711232 by=6530176
cntr i=2 acc=52241408 by=6530176
cntr i=1 acc=58771584 by=6530176
cntr i=0 acc=65301760 by=6530176
65301760
More information about the Digitalmars-d-learn
mailing list