Question

When a code is tail call optimized, is it superior in performance/complexity, consuming less CPU resources in relation to it's non-optimized counterpart, or does it only save memory and nothing else?

Was it helpful?

Solution

It might be a good idea to consult a particular processor specification, but from the general point of view tail call elimination improves performance because

  • no new stack frame is allocated
  • the nested call retuns directly to the caller of the current call rather than to the current call and only then to the original caller
  • less memory allocation means better use of CPU cache
  • less memory allocation means better use of main memory that may need to be paged or loaded when the stack is too large

The modern processors can reduce the overhead caused by these operations though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top