Use Java recursion with caution

Source: Internet
Author: User

The following describes how to cause a java. lang. StackOverflowError, that is, stack overflow:
[Java]
Public class RecursionTest
{
 
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}

Public static void main (String [] args)
{
Int totalTimes = 1000000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}
 
}

Public class RecursionTest
{

Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
 
Public static void main (String [] args)
{
Int totalTimes = 1000000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}

} Modify "totalTimes". When a certain value is reached, the following error is reported:


During development, you should avoid this problem, especially when there are too many recursive calls, it is best to replace it with for or whlie.
The following cycle () method:
[Java]
Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
}

Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
} Replace the recuresion () method:
[Java]
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}

Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}

[Java]
 

Let's take a look at the impact on program execution efficiency:
In the preceding example, the cycle () method and the following code are added to the main () method:
[Java]
StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("--------------------

StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("-------------------- the entire code is as follows:
[Java]
Public class RecursionTest
{
 
Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}

Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
}

Public static void main (String [] args)
{
Int totalTimes = 100000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");

StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}
 
}

Public class RecursionTest
{

Public static void recursion (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
TotalTimes --;
Time ++;
Recursion (totalTimes, time );
}
Else
{
System. out. println ("the call has ended." + time + "times ");
Return;
}
}
 
Public static void cycle (int totalTimes, int time)
{
If (totalTimes> 1)
{
System. out. println ("this is the" + time + "call! ");
}
Else
{
System. out. println ("the call has ended." + time + "times ");
}
}
 
Public static void main (String [] args)
{
Int totalTimes = 100000;
Int time = 1;
Long startTime = System. currentTimeMillis ();
System. out. println ("nested call start time:" + startTime );
Recursion (totalTimes, time );
System. out. println ("nested call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");

StartTime = System. currentTimeMillis ();
System. out. println ("cyclic call start time:" + startTime );
For (int index = totalTimes; index> 0; index --)
{
Cycle (index, time );
Time ++;
}
System. out. println ("loop call end time:" + System. currentTimeMillis ());
System. out. println ("Total time consumed:" + (System. currentTimeMillis ()-startTime ));
System. out. println ("------------------------------------------------------------");
}

}


 

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.