Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's an example of generating an input file like the one you describe that's 10000 lines long, then reading it back in and doing the calculation you posted and printing the result to stdout. I specifically disable any buffering when reading the file in order to get the worst possible read performance. I'm also not doing any caching at all, as others have suggested. The entire process, including generating the file, doing the calculation, and printing the results, consistently takes around 520-550 ms. That's hardly "slow", unless you're repeating this same process for hundreds or thousands of files. If you see drastically different performance from this, then maybe it's a hardware problem. A failing hard disk can drop read performance to nearly nothing.</p> <pre><code>import java.io.*; import java.util.Random; public class ReadingDoublesFromFileEfficiency { private static Random random = new Random(); public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); String filePath = createInputFile(); BufferedReader reader = new BufferedReader(new FileReader(filePath), 1); String line; while ((line = reader.readLine()) != null) { String[] details = line.split(","); double score = (Double.parseDouble(details[0]) * Double.parseDouble(details[1])) + Double.parseDouble(details[2]) + Double.parseDouble(details[3]) + Double.parseDouble(details[6]); System.out.println(score); } reader.close(); long elapsed = System.currentTimeMillis() - start; System.out.println("Took " + elapsed + " ms"); } private static String createInputFile() throws IOException { File file = File.createTempFile("testbed", null); PrintWriter writer = new PrintWriter(new FileWriter(file)); for (int i = 0; i &lt; 10000; i++) { writer.println(randomLine()); } writer.close(); return file.getAbsolutePath(); } private static String randomLine() { return String.format("%f,%f,%f,%f,%s,%s,%f", score(), score(), score(), score(), name(), name(), score()); } private static String name() { String name = ""; for (int i = 0; i &lt; 10; i++) { name += (char) (random.nextInt(26) + 97); } return name; } private static double score() { return random.nextDouble() * 100; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload