how can count recursion function calls in java

Discussion in 'Java' started by ziki, Oct 4, 2005.

  1. ziki

    ziki Guest

    Hi guys,
    I have a little question:
    I have recursion function in my program (java program) , How can I
    count the recursion calls that this function has made.

    thanks in advance,
    ziki, Oct 4, 2005
    #1
    1. Advertising

  2. "ziki" <> schreef in bericht
    news:...
    > Hi guys,


    Hi.

    > I have a little question:
    > I have recursion function in my program (java program) , How can I
    > count the recursion calls that this function has made.


    If the recursive algorithm is ever only executed by one thread, you can
    increment a static variable in the enclosing class. Otherwise you'd have to
    set up counts for each thread using ThreadLocal. Don't forget to reset the
    counter when the recursion ends.
    Boudewijn Dijkstra, Oct 4, 2005
    #2
    1. Advertising

  3. ziki

    R.F. Pels Guest

    ziki wrote:

    > I have recursion function in my program (java program) , How can I
    > count the recursion calls that this function has made.


    Homework???

    public void recurse(int depth)
    {
    ++depth;
    if (depth < 10)
    {
    recurse(depth);
    }
    }

    --
    Ruurd
    ..o.
    ...o
    ooo
    R.F. Pels, Oct 4, 2005
    #3
  4. On Tue, 4 Oct 2005 23:37:05 +0200, Boudewijn Dijkstra wrote:
    > "ziki" <> schreef in bericht
    >> I have recursion function in my program (java program) , How can I
    >> count the recursion calls that this function has made.

    >
    > If the recursive algorithm is ever only executed by one thread, you
    > can increment a static variable in the enclosing class. Otherwise
    > you'd have to set up counts for each thread using ThreadLocal.


    In order to avoid threading issues and the need to reset the state
    afterwards, I'd rather suggest using an accumulator (a common idiom
    with recursion):

    1. if the method doesn't already have something to return, keep track
    of and return the depth with a primitive:

    int recursiveMethod(int foo, int depth) {
    if (isBaseCase(foo)) {
    return depth;
    }
    else {
    return recursiveMethod(int foo, depth+1);
    }
    }

    2. if the method already uses the return value for something useful,
    pass an object instead:

    MyData recursiveMethod(int foo, Depth d) {
    if (isBaseCase(foo)) {
    return baseValue;
    }
    else {
    return recursiveMethod(foo, d.increment());
    }
    }

    You won't have to change any of the code that calls your function if
    you use a wrapper:

    MyData myFunction(int foo) {
    Depth d = new Depth(0);
    MyData r = myFunctionHelper(foo, d);
    System.out.println("Depth was " + d.getDepth());
    return r;
    }

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 5, 2005
    #4
  5. ziki

    Oliver Wong Guest

    "R.F. Pels" <> wrote in message
    news:dhusq3$g6r$1.ov.home.nl...
    > Homework???
    >
    > public void recurse(int depth)
    > {
    > ++depth;
    > if (depth < 10)
    > {
    > recurse(depth);
    > }
    > }


    Would have been funnier (to me, anyway) if you had written:

    homework?"Figure it out yourself.":"public void recurse(int
    depth){++depth;if (depth < 10)recurse(depth);}"

    - Oliver
    Oliver Wong, Oct 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    895
    Bengt Richter
    Aug 1, 2005
  2. slim_slim_cat

    Count string length by recursion!!

    slim_slim_cat, Nov 12, 2006, in forum: C Programming
    Replies:
    1
    Views:
    5,009
    yingmu
    Nov 13, 2006
  3. Jason Cavett

    Expensive Recursion + Method Calls?

    Jason Cavett, Oct 23, 2008, in forum: Java
    Replies:
    7
    Views:
    347
    Jason Cavett
    Oct 24, 2008
  4. Bob
    Replies:
    5
    Views:
    249
  5. Replies:
    8
    Views:
    721
    John Reye
    Apr 26, 2012
Loading...

Share This Page