We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
tl;dr = I see how the overhead for Scanner is significanty more than the overhead for BufferedReader. However, the different techniques for reading the input - whole line vs. one value at a time - make almost no difference. The same work has to be done either way.
Testing the runtimes is a great suggestion, but I think this is a more helpful comparison that would have better illustrated your point:
Scanner (overhead only) - 28.667 milliseconds (average) on a simple test case
BufferedReader obviously takes a lot less time to setup - 28.334 milliseconds less!
However, we were mainly discussing the approach to reading the entire array as one line vs. one value at a time. Check out the change in runtimes when we read the first array:
Scanner - 31.667 milliseconds (average) on a simple test case - increase of 3 milliseconds
Scannerin=newScanner(System.in);inttestCount=in.nextInt();intarraySize=in.nextInt();for(inti=1;i<=arraySize;i++){System.out.println(in.nextInt());// Calculations would go here}in.close();
BufferedReader - 3.333 milliseconds (average) on a simple test case - increase of 3 milliseconds
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));inttestCount=Integer.parseInt(in.readLine());intarraySize=Integer.parseInt(in.readLine());int[]array=newint[arraySize];inti=0;for(Stringinteger:in.readLine().split(" ")){array[i++]=Integer.parseInt(integer);}for(intj=0;j<arraySize;j++){System.out.println(array[j]);// Calculations would go here}in.close();
You can see that the different approaches to reading the actual values of the array don't make much difference at all. In fact, they're pretty much doing the same thing once you get down to it. It's really all about the overhead.
Thanks tat_lim, for following through with your explanation. I definitely learned something interesting!
(Note: BufferedReader also throws an IOException that has to be dealt with somehow. Not a big deal for our examples, but worth noting if you get an error trying to use it. This is probably why Scanner is the recommended class to start with for reading input - it's much easier to use.)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Maximum Subarray
You are viewing a single comment's thread. Return to all comments →
tl;dr = I see how the overhead for Scanner is significanty more than the overhead for BufferedReader. However, the different techniques for reading the input - whole line vs. one value at a time - make almost no difference. The same work has to be done either way.
Testing the runtimes is a great suggestion, but I think this is a more helpful comparison that would have better illustrated your point:
Scanner (overhead only) - 28.667 milliseconds (average) on a simple test case
BufferedReader (overhead only) - 0.333 milliseconds (average) on a simple test case
BufferedReader obviously takes a lot less time to setup - 28.334 milliseconds less!
However, we were mainly discussing the approach to reading the entire array as one line vs. one value at a time. Check out the change in runtimes when we read the first array:
Scanner - 31.667 milliseconds (average) on a simple test case - increase of 3 milliseconds
BufferedReader - 3.333 milliseconds (average) on a simple test case - increase of 3 milliseconds
You can see that the different approaches to reading the actual values of the array don't make much difference at all. In fact, they're pretty much doing the same thing once you get down to it. It's really all about the overhead.
Thanks tat_lim, for following through with your explanation. I definitely learned something interesting!
(Note: BufferedReader also throws an IOException that has to be dealt with somehow. Not a big deal for our examples, but worth noting if you get an error trying to use it. This is probably why Scanner is the recommended class to start with for reading input - it's much easier to use.)