Title: Not sure how to use System.nanoTime() for my program
Tags: java;fibonacci
Question: 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);
}
```

}

Comment: 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`.


Here is another answer: 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
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:02/05/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><fibonacci>', 'Title': 'Not sure how to use System.nanoTime() for my program', 'ViewCount': 1303, 'id': 54526373, 'user_AboutMe': None, 'user_AccountId': 15266893.0, 'user_DisplayName': 'Joshua Freeman', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 11015232.0}}