How to check how many times a recursive function has been called in Java?

stack man

Supposing a recursive function, for instance:

public static int factorial(int n){
    if(n==0){
        return 1;
    } else return n*factorial(n-1);
} 

How can I know how many times has been called for each parameter and store them in a map? (i.e: 10 times for n=5, 24 times for n=9 etc...)

EDIT:

Suppose the goal is to call it from a method like this, and it could be called many times within the program:

public Map<Integer, Integer> getTheNumberOfOccasionsItHasBeenCalled();
fvu

Basic solution for the case where you want to call the number of recursions for every possible input value (which in the case of factorial isn't that useful, but it could be in other cases):

static int thenumberoftimesiwascalled;

public static int factorial(int n){
    thenumberoftimesiwascalled++;
    if(n==0){
        return 1;
    } else return n*factorial(n-1);
} 

...


Map<Integer,Integer> thenumberoftimeshewascalled = new HashMap<>();
for (int i=1;i+100;i++) {
   // reset counter
   thenumberoftimesiwascalled = 0;
   // calculate
   int result = factorial(i);
   System.out.println("called " + thenumberoftimesiwascalled + " times for " + i);
   // stash in Map
   thenumberoftimeshewascalled.put(i,result);
}

Doing something useful with the map is left as an exercise, minimal version

static public Map<Integer, Integer> getNumberOfTimesCalled() {
   return thenumberoftimeshewascalled; 
}

Other interpretation, if you only want to count the number of times the function was called externally and not the number of recursions:

Map<Integer,Integer> externalcallspervalue = new HashMap<>();

// the actual function, renamed to hide avoid you having to change 
// in all the places it was called:

private static int internalfactorial(int n){
    if(n==0){
        return 1;
    } else return n*factorial(n-1);
} 

// and a simple wrapper that does the accounting - and has the same name
// and signature of the original function.

public static int factorial(int n){
   // do the accounting
   Integer ntc = externalcallspervalue.get(i);
   if (ntc==null) { // first time we see this value
      ntc=1;
   } else {
      ntc += 1;
   }
   externalcallspervalue.put(i,ntc);
   // and return the result by calling the hidden internal function
   return internalfactorial(i);
}

Now

for (int i=1;i+100;i++) {
   int result = factorial(i);
}

Will give you 1:1 2:1 3:1 as each value was called once.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check how many times a recursive function has been called in Java?

From Dev

How to test how many times a function has been called

From Dev

How to test how many times a function has been called

From Dev

Counting how many times a function has been called recursivly in JS

From Dev

How would you implement a javascript function so that it has a notion of how many times it has been called?

From Java

Keep track of how many times a recursive function was called

From Dev

How can I check if an anonymous function has been called with NSubstitute?

From Dev

Scheme: How to check if a function has been called with a same argument

From Java

is it possible to store method name in an array, and count how many times it has been called?

From Dev

is it possible to store method name in an array, and count how many times it has been called?

From Dev

How to check if a certain method has been called?

From Dev

Trying to track how many times java script function is called

From Dev

How to determine if a function has been called in Powershell

From Dev

Mapping collection based on how many times item been called

From Dev

Mapping collection based on how many times item been called

From Dev

How do you access the number of times a delegate has been called?

From Dev

How to add a variable to count the number of times the script has been called?

From Dev

How do you access the number of times a delegate has been called?

From Dev

How to add a variable to count the number of times the script has been called?

From Dev

Symfony MessageHandler count how many times a message has been dispatched

From Dev

How many times split has been used in breaking a sentence

From Dev

Keeping track of how many times a node has been visited

From Dev

Determine how many times a specific OS has been booted

From Dev

rails save how many times a page has been viewed

From Dev

Excel: How many times has a cell been referred to?

From Dev

Check if the function has been called in gtest

From Dev

Perl: How to check if CGI::header has already been called?

From Dev

How many times a reducer is called

From Dev

Counting how many times a function is called recursively (Python)

Related Related

  1. 1

    How to check how many times a recursive function has been called in Java?

  2. 2

    How to test how many times a function has been called

  3. 3

    How to test how many times a function has been called

  4. 4

    Counting how many times a function has been called recursivly in JS

  5. 5

    How would you implement a javascript function so that it has a notion of how many times it has been called?

  6. 6

    Keep track of how many times a recursive function was called

  7. 7

    How can I check if an anonymous function has been called with NSubstitute?

  8. 8

    Scheme: How to check if a function has been called with a same argument

  9. 9

    is it possible to store method name in an array, and count how many times it has been called?

  10. 10

    is it possible to store method name in an array, and count how many times it has been called?

  11. 11

    How to check if a certain method has been called?

  12. 12

    Trying to track how many times java script function is called

  13. 13

    How to determine if a function has been called in Powershell

  14. 14

    Mapping collection based on how many times item been called

  15. 15

    Mapping collection based on how many times item been called

  16. 16

    How do you access the number of times a delegate has been called?

  17. 17

    How to add a variable to count the number of times the script has been called?

  18. 18

    How do you access the number of times a delegate has been called?

  19. 19

    How to add a variable to count the number of times the script has been called?

  20. 20

    Symfony MessageHandler count how many times a message has been dispatched

  21. 21

    How many times split has been used in breaking a sentence

  22. 22

    Keeping track of how many times a node has been visited

  23. 23

    Determine how many times a specific OS has been booted

  24. 24

    rails save how many times a page has been viewed

  25. 25

    Excel: How many times has a cell been referred to?

  26. 26

    Check if the function has been called in gtest

  27. 27

    Perl: How to check if CGI::header has already been called?

  28. 28

    How many times a reducer is called

  29. 29

    Counting how many times a function is called recursively (Python)

HotTag

Archive