Hussein Zawawi
Hussein Zawawi

Reputation: 2937

Scanner in java performance

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?

Upvotes: 2

Views: 320

Answers (4)

Aleksandr Lovkov
Aleksandr Lovkov

Reputation: 65

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

Upvotes: 0

Polynomial
Polynomial

Reputation: 28316

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?

Upvotes: 1

shift66
shift66

Reputation: 11958

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

Upvotes: 0

Android Killer
Android Killer

Reputation: 18499

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

Upvotes: 2

Related Questions