home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!rutgers!princeton!csservices!kastle!blume
- From: blume@kastle.Princeton.EDU (Matthias Blume)
- Newsgroups: comp.lang.c
- Subject: Re: To Recurse or NOT to recurse?
- Message-ID: <1992Nov7.205704.19368@csservices.Princeton.EDU>
- Date: 7 Nov 92 20:57:04 GMT
- References: <1992Nov6.185136.8055@netcom.com> <geyke2W00VQsEhTcIK@andrew.cmu.edu>
- Sender: news@csservices.Princeton.EDU (USENET News System)
- Reply-To: blume@kastle.Princeton.EDU (Matthias Blume)
- Organization: Dept. of Computer Science, Princeton University
- Lines: 28
-
- In article <geyke2W00VQsEhTcIK@andrew.cmu.edu>, Richard Dale Romero
- <rickr+@CMU.EDU> writes:
- |> Excerpts from netnews.comp.lang.c: 6-Nov-92 To Recurse or NOT to
- |> recurse? Stephen Schow@netcom.com (577)
- |>
- |> > What is better to do when traversing a linked list..... SHould I use
- |> > recursion to search down through the list for the last node, or should I
- |> use a while loop? What or the pro's and con's of each?
- |>
- |> Some compilers are really smart, and they will turn your code into (if
- |> it is tail recursion) into a while loop. Others won't. If it doesn't,
- |> are you wasting space? Yes. How much? Well, you're using O(N) space
- |> when it can be done in O(1) space. That's a lot as far as I'm
- |> concerned. And speed? Bit trickier, and dependent on the machine you
- |> use, but I'd lay odds on the while loop, say 10:1, that it'd be faster.
- |>
- |> -rick
- |>
- |>
-
- I would not count on such a ``smart'' C compiler, because there are lots
- of issues, which can prevent it from generating loop code for a tail recursive
- function. (Think about addresses of local variables, setjmp/longjmp and
- alloca).
- In a language like Scheme, which is *defined* to produce code, that runs in
- constant space for tail recursions, you can (and in fact must) count on it.
-
- -Matthias
-