- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSequencesTiming.java
66 lines (56 loc) · 2.74 KB
/
SequencesTiming.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
packagecom.jwetherell.algorithms.sequence.timing;
importjava.text.DecimalFormat;
importcom.jwetherell.algorithms.sequence.FibonacciSequence;
importcom.jwetherell.algorithms.sequence.ArithmeticProgression;
publicclassSequencesTiming {
privatestaticfinalDecimalFormatFORMAT = newDecimalFormat("#.######");
publicstaticvoidmain(String[] args) {
{
// TOTAL OF A SEQUENCE OF NUMBERS
intstart = 14;
intlength = 10000;
System.out.println("Computing sequence total using a loop.");
longbefore = System.nanoTime();
ArithmeticProgression.sequenceTotalUsingLoop(start, length);
longafter = System.nanoTime();
System.out.println("Computed in " + FORMAT.format(after - before) + " ns");
System.gc();
System.out.println("Computing sequence total using Triangular Numbers.");
before = System.nanoTime();
ArithmeticProgression.sequenceTotalUsingTriangularNumbers(start, length);
after = System.nanoTime();
System.out.println("Computed in " + FORMAT.format(after - before) + " ns");
System.out.println();
System.gc();
}
{
// COMPUTE FIBONACCI SEQUENCE
System.out.println("Computing Fibonacci sequence total using a loop.");
intelement = 25;
longbefore = System.nanoTime();
FibonacciSequence.fibonacciSequenceUsingLoop(element);
longafter = System.nanoTime();
System.out.println("Computed in " + FORMAT.format(after - before) + " ns");
System.gc();
System.out.println("Computing Fibonacci sequence total using Recursion.");
before = System.nanoTime();
FibonacciSequence.fibonacciSequenceUsingRecursion(element);
after = System.nanoTime();
System.out.println("Computed in " + FORMAT.format(after - before) + " ns");
System.gc();
System.out.println("Computing Fibonacci sequence total using Matrix.");
before = System.nanoTime();
FibonacciSequence.fibonacciSequenceUsingMatrixMultiplication(element);
after = System.nanoTime();
System.out.println("Computed in " + FORMAT.format(after - before) + " ns");
System.gc();
System.out.println("Computing Fibonacci sequence total using Binet's formula.");
before = System.nanoTime();
FibonacciSequence.fibonacciSequenceUsingBinetsFormula(element);
after = System.nanoTime();
System.out.println("Computed in " + FORMAT.format(after - before) + " ns");
System.out.println();
System.gc();
}
}
}