2

I have a file with 100000 line when am using System.in it takes more than 1 minute to get the input but when I use a file to read the input it doesn't take time. what is the solution to keep using System.in but with more speed?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Hussein Zawawi
  • 2,837
  • 2
  • 24
  • 44

4 Answers4

2

System.in reads line by line not the file contents at a time.So it is slow.

Android Killer
  • 17,747
  • 12
  • 64
  • 88
1

System.in is inherently slow because it's taking the data in line by line (checking for newlines), rather than doing a large block copy from a file mapped in virtual memory.

There's no real way to speed up System.in, and this sounds like a situation where reading a file would be much more ideal.

Update: You may want to look at this question: What's the fastest way to read from System.in in Java?

Community
  • 1
  • 1
Polynomial
  • 26,720
  • 10
  • 77
  • 106
0

You need to use StringTokenizer if you want to read file very fast. https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

0

Try to change the stream of system.in
System.setIn(new FileInputStream(fileName));

shift66
  • 11,440
  • 11
  • 47
  • 78