Skip to content

Recursive or Looping

July 1, 2011

package com.jagan.bigO;

import java.math.BigInteger;

import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

public class Factorial {

public static void main(String[] args) {
System.out.println(getFact_recursive(new BigInteger(“20”)));
System.out.println(getFact_looping(new BigInteger(“20”)));

}

public static BigInteger getFact_recursive(BigInteger fCount) {
StopWatch stopWatch = new Log4JStopWatch();
BigInteger result = new BigInteger(“1”);

if (fCount.compareTo(new BigInteger(“0”)) == 1)
result = fCount.multiply(getFact_recursive(fCount
.subtract(new BigInteger(“1”))));
// result= (fCount>0)?fCount * getFiba_recursive(fCount-1):1;
stopWatch.stop(“recursive”);
return result;

}

public static BigInteger getFact_looping(BigInteger fCount) {
StopWatch stopWatch = new Log4JStopWatch();
BigInteger result = new BigInteger(“1”);
for (BigInteger bi = fCount; bi.compareTo(BigInteger.ZERO) > 0; bi = bi
.subtract(BigInteger.ONE)) {

result = result.multiply(bi);
}
stopWatch.stop(“looping”);
return result;

}
}

The above program depends on perf4j.

Maven Dependency…

<dependency>
<groupId>org.perf4j</groupId>
<artifactId>perf4j</artifactId>
<version>0.9.14</version>
</dependency>

Performance Statistics of 3 different runs….

Performance Statistics   2011-07-01 10:22:10 – 2011-07-01 10:22:20
Tag                                                  Avg(ms)         Min         Max     Std Dev       Count
looping                                                  0.0           0           0         0.0           1
recursive                                                0.8           0           1         0.4          21

Performance Statistics   2011-07-01 10:24:40 – 2011-07-01 10:24:50
Tag                                                  Avg(ms)         Min         Max     Std Dev       Count
looping                                                  0.0           0           0         0.0           1
recursive                                                1.2           0           2         0.5          21

Performance Statistics   2011-07-01 10:28:00 – 2011-07-01 10:28:10
Tag                                                  Avg(ms)         Min         Max     Std Dev       Count
looping                                                  0.0           0           0         0.0           1
recursive                                                0.7           0           1         0.5          21

We can decide which one is better!

The reason for using BigInteger is, to find Factorial of  a bigger number and to get notable changes in the Perf Stat…..

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: