# Recursion Method

**URL:** https://discussions.unity.com/t/recursion-method/598425
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 21, 2015, 3:10pm UTC](https://discussions.unity.com/t/recursion-method/598425 "2015-09-21T15:10:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kainanteh](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kainanteh/32/556610_2.png) [@Kainanteh](https://discussions.unity.com/u/Kainanteh)
#### Post date: [September 21, 2015, 3:10pm UTC](https://discussions.unity.com/t/recursion-method/598425/1 "2015-09-21T15:10:17Z")

</div>

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

```csharp
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;
       
    }

}

```

---

<div class="post-metadata">

### Author: ![Baste](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/baste/32/880289_2.png) [@Baste](https://discussions.unity.com/u/Baste)
#### Post date: [September 21, 2015, 5:40pm UTC](https://discussions.unity.com/t/recursion-method/598425/2 "2015-09-21T17:40:14Z")

</div>

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:

```auto
    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.

```

---

<div class="post-metadata">

### Author: ![Kainanteh](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kainanteh/32/556610_2.png) [@Kainanteh](https://discussions.unity.com/u/Kainanteh)
#### Post date: [September 21, 2015, 6:23pm UTC](https://discussions.unity.com/t/recursion-method/598425/3 "2015-09-21T18:23:14Z")

</div>

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