0

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);
}

}

vs97
  • 5,765
  • 3
  • 28
  • 41
  • You would wrap the code you want to time with the lines you have indicated. Probably this would be either `main` as a whole or each iteration of the loop that calls `fibonacci`. – Thilo Feb 05 '19 at 00:50

1 Answers1

0

I assume you are profiling how long your code takes?

If so, you start the timer before everything you want to measure, then you record after you want to measure, and then you find the difference. Just pretend you are using a stopwatch to time someone... same thing here.

This means you can do:

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:");

        long startTime = System.nanoTime();
        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
        }
        long endTime = System.nanoTime();

        System.out.println("It took " + n + " iterations: " + (endTime - startTime) + " nanoseconds");
    }

    //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);
    }
}

Note however that this code is timing the printing, which is probably a pretty slow operation and not a good measurement of how long the function is taking. If you want to time it, you should only time the function, and then add the difference to a sum, and then report the sum at the end.

In pseudocode, this means:

long sum = 0;
for (int i = 0 ...) {
    long start = recordTime();
    Object result = doCalculation();
    sum += recordTime() - start;
    // Do something with result here, like print it so you don't time  printing
}
// `sum` now contains your time without interference of the loop or printing
Water
  • 3,245
  • 3
  • 28
  • 58