Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Recursion Method

Discussion in 'Scripting' started by Kainanteh, Sep 21, 2015.

  1. Kainanteh

    Kainanteh

    Joined:
    Sep 3, 2014
    Posts:
    14
    I have a problem of efficiency, this recursive method is very slow (especially with high input values) but I don't see the problem.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Prueba : MonoBehaviour {
    5.  
    6.  
    7.     int Money = 1000000;
    8.  
    9.  
    10.     void OnMouseUp()
    11.     {
    12.        
    13.         Bill_filter(Money);
    14.        
    15.        
    16.     }
    17.  
    18.  
    19.     public int Bill_filter(int rest)
    20.     {
    21.        
    22.        
    23.         Debug.Log("enter");
    24.        
    25.        
    26.         if(Money - 500 == 0)
    27.         {
    28.            
    29.            
    30.             Debug.Log(" 1 de 500 " + " and rest : " + rest );
    31.            
    32.            
    33.             return 0;  
    34.            
    35.         }
    36.         else if(rest - 500 >= 0)
    37.         {
    38.            
    39.            
    40.             Debug.Log(" 1 de 500 " + " and rest : " + rest );
    41.             Bill_filter(rest - 500);
    42.            
    43.             return 0;  
    44.            
    45.         }
    46.         if(Money - 200 == 0)
    47.         {
    48.            
    49.            
    50.             Debug.Log(" 1 de 200 " + " and rest : " + rest );
    51.            
    52.            
    53.             return 0;  
    54.            
    55.         }
    56.         else if(rest - 200 >= 0)
    57.         {
    58.            
    59.            
    60.             Debug.Log(" 1 de 200 " + " and rest : " + rest );
    61.             Bill_filter(rest - 200);
    62.            
    63.             return 0;  
    64.            
    65.         }
    66.         if(Money - 100 == 0)
    67.         {
    68.            
    69.            
    70.             Debug.Log(" 1 de 100 " + " and rest : " + rest );
    71.            
    72.            
    73.             return 0;  
    74.            
    75.         }
    76.         else if(rest - 100 >= 0)
    77.         {
    78.            
    79.            
    80.             Debug.Log(" 1 de 100 " + " y and rest : " + rest );
    81.             Bill_filter(rest - 100);
    82.            
    83.             return 0;  
    84.            
    85.         }
    86.         if(Money - 50 == 0)
    87.         {
    88.            
    89.            
    90.             Debug.Log(" 1 de 50 " + " and rest : " + rest );
    91.            
    92.            
    93.             return 0;  
    94.            
    95.         }
    96.         else if(rest - 50 >= 0)
    97.         {
    98.            
    99.            
    100.             Debug.Log(" 1 de 50 " + " and rest : " + rest );
    101.             Bill_filter(rest - 50);
    102.            
    103.             return 0;  
    104.            
    105.         }
    106.         if(Money - 20 == 0)
    107.         {
    108.            
    109.            
    110.             Debug.Log(" 1 de 20 " + " and rest : " + rest );
    111.            
    112.            
    113.             return 0;  
    114.            
    115.         }
    116.         else if(rest - 20 >= 0)
    117.         {
    118.            
    119.            
    120.             Debug.Log(" 1 de 20 " + " and rest : " + rest );
    121.             Bill_filter(rest - 20);
    122.            
    123.             return 0;  
    124.            
    125.         }
    126.         else if(Money - 10 == 0)
    127.         {
    128.            
    129.            
    130.             Debug.Log(" 1 de 10 " + " and rest : " + rest );
    131.            
    132.            
    133.             return 0;  
    134.            
    135.         }
    136.         else if(rest - 10 >= 0)
    137.         {
    138.            
    139.             Debug.Log(" 1 de 10 " + " and rest : " + rest );
    140.             Bill_filter(rest - 10);
    141.            
    142.             return 0;
    143.            
    144.         }
    145.         else if(rest - 5 == 0)
    146.         {
    147.            
    148.            
    149.             Debug.Log(" 1 de 5 " + " and rest : " + rest );
    150.            
    151.             return 0;
    152.            
    153.         }
    154.        
    155.         Debug.Log("exit");
    156.         return 0;
    157.        
    158.     }
    159.  
    160.  
    161. }
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    It's because of the Debug.Log's

    Debug.Log (and LogWarning and LogFormat) creates a complete stack trace. That takes a lot of time.

    turn the method into one that builds a complete string, and print that after you're done, and you'll be good:

    Code (csharp):
    1.  
    2.     void OnMouseUp()
    3.     {
    4.         string result = Bill_filter(Money);
    5.         Debug.Log(result);
    6.     }
    7.  
    8.  
    9.     public string Bill_filter(int rest)
    10.     {
    11.         if(Money - 500 == 0)
    12.         {
    13.              return " 1 de 500 " + " and rest : " + rest;
    14.         }
    15.         else if(rest - 500 >= 0)
    16.         {
    17.              return (" 1 de 500 " + " and rest : " + rest ) + "\n" + Bill_filter(rest - 500);
    18.         }
    19.        
    20.         etc.
    21.  
     
    LeftyRighty and Kainanteh like this.
  3. Kainanteh

    Kainanteh

    Joined:
    Sep 3, 2014
    Posts:
    14
    That was exactly the problem, I was thinking all the time in the recursion, Thanks you're the boss