algorithm - Java Fibonacci - elapsedTime(); formula -


i'm working on small project fibonacci algorithm.

i'm using following method calculate algorithm. note elapsedtime() returns double.

public static void fibonaccisequence(long n1, long n2) {      t0 = stopwatch.elapsedtime();     system.out.print("index: " + index + " -> " + n1 + "\t");     t1 = stopwatch.elapsedtime();     laptime = (1000 * t1 - 1000 * t0) / 1000;     stdout.println(" (" + laptime + "\t " + t1 + ")");      if (index == stoppingpoint) {         return;     }     index++;     fibonaccisequence(n2, n1 + n2); } 

now don't pay attention algorithm - gets fixed. don't understand formula laptime. why can't be

laptime = t1-t0;  

this belongs stopwatch class java princeton stdlib. class meant measure time of execution of algorithm. implementation goes (removed comments , such data):

public class stopwatch {       private final long start;      public stopwatch() {         start = system.currenttimemillis();     }       public double elapsedtime() {         long = system.currenttimemillis();         return (now - start) / 1000.0;     } } 

since uses system.currenttimemillis() work milliseconds. elapsedtime method convert seconds double. current formula:

laptime = (1000 * t1 - 1000 * t0) / 1000; 

is making sure convert data pure double operation. so, formula can rewritten as:

laptime = t1 - t0; 

with no problem.

note still this not right way measure time of execution java code. should use system.nanotime() instead.

more info:


going deeper understand if there's no difference between these operations, let's create basic test:

public class formulatest {     static double formula1(double t0, double t1) {         return (1000 * t1 - 1000 * t0) / 1000;     }      static double formula2(double t0, double t1) {         return t1 - t0;     }      static void printresults(double t0, double t1) {         system.out.println("t0: " + t0);         system.out.println("t1: " + t1);         system.out.println("formula1: " + formula1(t0, t1));         system.out.println("formula2: " + formula1(t0, t1));         system.out.println("---------------------------------------------------");     }      public static void main(string[] args) throws java.lang.exception {         // code goes here         printresults(0, 10);         printresults(system.currenttimemillis(), system.currenttimemillis());         printresults(system.currenttimemillis(), system.currenttimemillis() + 142);         printresults(1.7976931348623157e+300 - 5000, 1.7976931348623157e+300);         printresults(             109999999999999999999999999999999999999999999999999999999999999999999999.0,             119999999999999999999999999999999999999999999999999999999999999999999999.0);         printresults(             10.9999999999999999999999999999999999999999999999999999999999999999999999,             11.9999999999999999999999999999999999999999999999999999999999999999999999);         printresults(             823145321462149234.651985149616914621346234923149621346921394613293423951932415934159213226314,             844329146321496321.532159341563149513495139159341593415793415431951349513891585443951391593151);         printresults(             82314532.1462149234651985149616914621346234923149621346921394613293423951932415934159213226314,             84432914.6321496321532159341563149513495139159341593415793415431951349513891585443951391593151);         printresults(1.7976931348623157e+307, 4.9e-323);         printresults(double.max_value, double.max_value);         printresults(double.min_value - 10, double.min_value);     } } 

output:

t0: 0.0 t1: 10.0 formula1: 10.0 formula2: 10.0 --------------------------------------------------- t0: 1.410290897577e12 t1: 1.410290897577e12 formula1: 0.0 formula2: 0.0 --------------------------------------------------- t0: 1.410290897577e12 t1: 1.410290897719e12 formula1: 142.0 formula2: 142.0 --------------------------------------------------- t0: 1.7976931348623156e300 t1: 1.7976931348623156e300 formula1: 0.0 formula2: 0.0 --------------------------------------------------- t0: 1.1e71 t1: 1.2e71 formula1: 9.999999999999985e69 formula2: 9.999999999999985e69 --------------------------------------------------- t0: 11.0 t1: 12.0 formula1: 1.0 formula2: 1.0 --------------------------------------------------- t0: 8.2314532146214925e17 t1: 8.4432914632149632e17 formula1: 2.1183824859347024e16 formula2: 2.1183824859347024e16 --------------------------------------------------- t0: 8.231453214621492e7 t1: 8.443291463214964e7 formula1: 2118382.4859347227 formula2: 2118382.4859347227 --------------------------------------------------- t0: 1.7976931348623158e307 t1: 4.9e-323 formula1: -infinity formula2: -infinity --------------------------------------------------- t0: 1.7976931348623157e308 t1: 1.7976931348623157e308 formula1: nan formula2: nan --------------------------------------------------- t0: -10.0 t1: 4.9e-324 formula1: 10.0 formula2: 10.0 --------------------------------------------------- 

not @ edge cases double.max_value , double.min_value differences between results of formula. when result -infinity or nan (not number).

in short: use last formula:

laptime = t1 - t0; 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -