Computational fairy tale what makes this recursive add bad?

Status
Not open for further replies.

wtr

Full Member level 5
Joined
May 1, 2014
Messages
299
Helped
29
Reputation
58
Reaction score
25
Trophy points
1,308
Activity points
4,108
There is this book that I’ve been reading for a bit of fun.

Computational Fairy Tales by Jeremy Kubica.

Within this book there is a section called computational Graffiti.

The following snippet

Code C - [expand]
1
2
3
4
Int RecursiveAdd (int n, int m) {
  If (m == 0) return n;
  Return RecursiveAdd(n, m-1) +1;
}


The book makes jest at the inefficiencies of it all.

I haven’t got the means to compute this atm, I want to know why this is BAD.
I can see the function gets called m+1 times.

Doesn’t the recursive add work exactly the same way as for (i==1; i<m; i++) ?

Is it inefficient because i cpu could just use sum and carry register on a multi bit word and therefore do this all in one clock cycle? with opcode +

Regards,
Wes
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…