Recursion Method

I have a problem of efficiency, this recursive method is very slow (especially with high input values) but I don’t see the problem.

using UnityEngine;
using System.Collections;

public class Prueba : MonoBehaviour {


    int Money = 1000000;


    void OnMouseUp()
    {
       
        Bill_filter(Money);
       
       
    }


    public int Bill_filter(int rest)
    {
       
       
        Debug.Log("enter");
       
       
        if(Money - 500 == 0)
        {
           
           
            Debug.Log(" 1 de 500 " + " and rest : " + rest );
           
           
            return 0;   
           
        }
        else if(rest - 500 >= 0)
        {
           
           
            Debug.Log(" 1 de 500 " + " and rest : " + rest );
            Bill_filter(rest - 500);
           
            return 0;   
           
        }
        if(Money - 200 == 0)
        {
           
           
            Debug.Log(" 1 de 200 " + " and rest : " + rest );
           
           
            return 0;   
           
        }
        else if(rest - 200 >= 0)
        {
           
           
            Debug.Log(" 1 de 200 " + " and rest : " + rest );
            Bill_filter(rest - 200);
           
            return 0;   
           
        }
        if(Money - 100 == 0)
        {
           
           
            Debug.Log(" 1 de 100 " + " and rest : " + rest );
           
           
            return 0;   
           
        }
        else if(rest - 100 >= 0)
        {
           
           
            Debug.Log(" 1 de 100 " + " y and rest : " + rest );
            Bill_filter(rest - 100);
           
            return 0;   
           
        }
        if(Money - 50 == 0)
        {
           
           
            Debug.Log(" 1 de 50 " + " and rest : " + rest );
           
           
            return 0;   
           
        }
        else if(rest - 50 >= 0)
        {
           
           
            Debug.Log(" 1 de 50 " + " and rest : " + rest );
            Bill_filter(rest - 50);
           
            return 0;   
           
        }
        if(Money - 20 == 0)
        {
           
           
            Debug.Log(" 1 de 20 " + " and rest : " + rest );
           
           
            return 0;   
           
        }
        else if(rest - 20 >= 0)
        {
           
           
            Debug.Log(" 1 de 20 " + " and rest : " + rest );
            Bill_filter(rest - 20);
           
            return 0;   
           
        }
        else if(Money - 10 == 0)
        {
           
           
            Debug.Log(" 1 de 10 " + " and rest : " + rest );
           
           
            return 0;   
           
        }
        else if(rest - 10 >= 0)
        {
           
            Debug.Log(" 1 de 10 " + " and rest : " + rest );
            Bill_filter(rest - 10);
           
            return 0;
           
        }
        else if(rest - 5 == 0)
        {
           
           
            Debug.Log(" 1 de 5 " + " and rest : " + rest );
           
            return 0;
           
        }
       
        Debug.Log("exit");
        return 0;
       
    }


}

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:

    void OnMouseUp()
    {
        string result = Bill_filter(Money);
        Debug.Log(result);
    }


    public string Bill_filter(int rest)
    {
        if(Money - 500 == 0)
        {
             return " 1 de 500 " + " and rest : " + rest;
        }
        else if(rest - 500 >= 0)
        {
             return (" 1 de 500 " + " and rest : " + rest ) + "\n" + Bill_filter(rest - 500);
        }
       
        etc.
2 Likes

That was exactly the problem, I was thinking all the time in the recursion, Thanks you’re the boss