I need to measure the time in nanoseconds for my project but I'm not sure how to properly implement it into my program. I know I need to use something like:
long startTime = System.nanoTime();
...
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
but I'm not sure where I have to implement it so my program works properly. Here is my code:
import java.util.*;
public class fiboSeriesRec
{
public static void main(String[] args)
{
//Scanner allows input from user, int in this case
Scanner sc = new Scanner(System.in);
long n; //declare n as a long since numbers get too large for int
System.out.println("How many numbers 'n' do you wish to see?"); //Prompts the user to input a number
n = sc.nextInt();
System.out.println("The first " + n + " Fibonacci numbers are:");
for (long i=0; i < n; i++) //Adds each 'n' to a list as the output
{
System.out.println(fibonacci(i)); //Prints out the list
}
}
//Recursive function for fibonacci sequence
public static long fibonacci(long num) {
if (num == 0) {
return 0;
}
else if(num == 1)
{
return 1;
}
return fibonacci(num-1) + fibonacci(num-2);
}
}