KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm looking for the most time-efficient way to read STDIN line-by-line. The first line is the number of conditions to test. All the following lines are conditions (strings) with a maximum of 100 000 characters. I have already tried the following (plus result for 4 times 90 000 characters: Scanner with a while-loop (7255 ms) Scanner sc = new Scanner(System.in); int numberOfLines = Integer.parseInt(sc.nextLine()); long start = 0; int i = 1; while (i<=numberOfLines){ start = System.currentTimeMillis(); sc.nextLine(); Debug.println((System.currentTimeMillis()-start) + "ms for scanner while"); i++; } Results : 3228ms for scanner while 2264ms for scanner while 1309ms for scanner while 454ms for scanner while Scanner with a for-loop (7078 ms) Scanner sc = new Scanner(System.in); int numberOfLines = Integer.parseInt(sc.nextLine()); long start = 0; for (int i = 1; i<= numberOfLines;i++){ start = System.currentTimeMillis(); sc.nextLine(); Debug.println((System.currentTimeMillis()-start) + "ms for scanner for"); //i++; } Results : 3168ms for scanner for 2207ms for scanner for 1236ms for scanner for 467ms for scanner for BufferedReader with a for-loop (7403 ms) try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int numberOfLines = Integer.parseInt(br.readLine()); long start = 0; for (int i = 0; i< numberOfLines;i++){ start = System.currentTimeMillis(); br.readLine(); Debug.println((System.currentTimeMillis()-start) + "ms for bufferreader for"); //i++; } } catch (Exception e) { System.err.println("Error:" + e.getMessage()); } Results : 3273ms for bufferreader for 2330ms f
Tags (comma-separated)
Save Edits
Cancel