I've been working on a project to show that reading input using Scanner is slower than using buffered reader which is in turn is slower than using our own functions which parse integer using suitable manipulation. For this I'm noting down the execution time to read input in each case.
long startTime,endTime,duration;
startTime=System.nanoTime();
/* Reading input via scanner */
endTime=System.nanoTime();
duration = endTime - startTime;
System.out.println(duration);
startTime=System.nanoTime();
/* Reading input via buffered reader */
endTime=System.nanoTime();
duration = endTime - startTime;
System.out.println(duration);
startTime=System.nanoTime();
/* Reading input via own function for I/O */
endTime=System.nanoTime();
duration = endTime - startTime;
System.out.println(duration);
I'm getting runtime error in above code if I try running all three in a single program. However, if I use one method at a time, it works fine. I doubt there's some problem in my way to compute execution time. Please help.
Here's my code http://ideone.com/BwvAXr