An experiment of tail recursion

Source: Internet
Author: User

I was just. NET test, for the tail recursion, in debug mode, will not be optimized to non-recursive structure, in release mode, will be optimized to non-recursive structure, there is no stack overflow problem

Stst?

This is my simulation of the file structure class

Stst

?

This is a non-tailed recursive version

Stst

This is the tail-recursive version.

Stst

This is the test code.

Stst

This is the definition of createdir.

Stst

Results:

1, in debug mode, whether it is an ill-ending recursion, will overflow

2, in release mode, the non-tail recursive version will overflow, and the tail recursive version will not overflow

Stst

Modern compilers I think it's all there.

Stst

Because tail recursion is optimized for non-recursive versions, it's a very old technology.

Stst

You can test it under Java, I don't have a Java environment

Stst

Instead of changing your method parameter to the file array type, you can turn recursion into a real tail recursion.

Stst


There's no basis for this.

0xb000:do_ tail recursion (Files)

......

......

Stack:

0xa000:-> Parameters: Files

0xa004:-> return Address: 0xa000

0xa008:? Parameters: Files

0xa012:? --Return Address: 0xa004

0xa016:??? Parameters: Files

0xa020:??? --Return Address: 0xa012

............

The above is the recursive stack structure, the tail recursion has such a fact:

Is that each time recursion, its previous call has been calculated, no need to store intermediate results, so this call can fully use the previous call stack, so the stack structure does not extend downward

An experiment of tail recursion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.