Created
February 27, 2017 14:05
-
-
Save georgepowell/0bb67c768e661bda6bdbc8ab96de1a04 to your computer and use it in GitHub Desktop.
Timing the 2^n approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
for (int n = 0; n < 1000; n++) { | |
long startTime = System.nanoTime(); | |
fib(n); | |
long endTime = System.nanoTime(); | |
double duration = (endTime - startTime) / 1000000000.0; | |
System.out.println(n + ": " + duration + "s"); | |
} | |
} | |
static int fib(int n) { | |
return n < 2 ? 1 : fib(n - 1) + fib(n - 2); | |
} | |
} |
Author
georgepowell
commented
Feb 27, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment