java - Files.readAllLines() Execution Time Changes -
i playing around different ways read numbers file , how efficient are, here 1 method using:
public static long getnumbers1() { final long starttime = system.nanotime(); seek { string input = new string(files.readallbytes(file.topath())); string[] stringnumbers = input.split("\\w"); int[] numbers = new int[stringnumbers.length]; for(int index = 1;index < stringnumbers.length;index++) { numbers[index] = integer.parseint(stringnumbers[index]); } } grab (ioexception e) { e.printstacktrace(); } final long endtime = system.nanotime(); system.out.println(endtime + " | " + starttime + " | " + (endtime - starttime)); homecoming endtime - starttime; }
file
declared @ global scope:
private static file file = new file(system.getproperty("user.dir") + "/data/numtest.txt");
this method run next means:
for (int index = 0;index < 10;index++) { getnumbers1(); }
printed in console following:
15395409456370 | 15395397323226 | 12133144 15395410416178 | 15395410090933 | 325245 15395411137449 | 15395410835563 | 301886 15395411806342 | 15395411515427 | 290915 15395412389234 | 15395412097611 | 291623 15395412780660 | 15395412529737 | 250923 15395413168193 | 15395412912315 | 255878 15395413538738 | 15395413302679 | 236059 15395413948214 | 15395413665792 | 282422 15395414329376 | 15395414083762 | 245614
you notice first 'run time' value (the 3rd value) greater in first reading of file subsequent readings. no matter how many times run program, or how many times create loop run (100 or 100000) first value much greater. why happening? can prevent happening? java beingness smart , storing values file , isn't re-reading file each time?
i curious...
that disk caching @ work. first read coming off disk. sec read coming out of disk cache.
i've done performance testing on algorithms in past. file io , caching getting in way or effecting results. need think sort of performance you're looking for.
if testing finish system, maintain file io in there, need flush caches consistent results.
if testing algorithm, maintain io out of timers.
move 'starttime = system.nanotime()' after reading file.
java
No comments:
Post a Comment