Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>r1</code> is more efficient. The <code>InputStreamReader</code> itself doesn't have a large buffer. The <code>BufferedReader</code> can be set to have a larger buffer than <code>InputStreamReader</code>. The <code>InputStreamReader</code> in <code>r2</code> would act as a bottleneck. </p> <p>In a nut: you should read the data through a funnel, not through a bottle.</p> <hr> <p><strong>Update</strong>: here's a little benchmark program, just copy'n'paste'n'run it. You don't need to prepare files.</p> <pre><code>package com.stackoverflow.q3459127; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; public class Test { public static void main(String... args) throws Exception { // Init. int bufferSize = 10240; // 10KB. int fileSize = 100 * 1024 * 1024; // 100MB. File file = new File("/temp.txt"); // Create file (it's also a good JVM warmup). System.out.print("Creating file .. "); BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(file)); for (int i = 0; i &lt; fileSize; i++) { writer.write("0"); } System.out.printf("finished, file size: %d MB.%n", file.length() / 1024 / 1024); } finally { if (writer != null) try { writer.close(); } catch (IOException ignore) {} } // Read through funnel. System.out.print("Reading through funnel .. "); Reader r1 = null; try { r1 = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), bufferSize); long st = System.nanoTime(); for (int data; (data = r1.read()) &gt; -1;); long et = System.nanoTime(); System.out.printf("finished in %d ms.%n", (et - st) / 1000000); } finally { if (r1 != null) try { r1.close(); } catch (IOException ignore) {} } // Read through bottle. System.out.print("Reading through bottle .. "); Reader r2 = null; try { r2 = new InputStreamReader(new BufferedInputStream(new FileInputStream(file), bufferSize), "UTF-8"); long st = System.nanoTime(); for (int data; (data = r2.read()) &gt; -1;); long et = System.nanoTime(); System.out.printf("finished in %d ms.%n", (et - st) / 1000000); } finally { if (r2 != null) try { r2.close(); } catch (IOException ignore) {} } // Cleanup. if (!file.delete()) System.err.printf("Oops, failed to delete %s. Cleanup yourself.%n", file.getAbsolutePath()); } } </code></pre> <p>Results at my Latitude E5500 with a <a href="http://www.seagate.com/docs/pdf/datasheet/disc/ds_momentus_7200_3.pdf" rel="noreferrer">Seagate Momentus 7200.3</a> harddisk:</p> <pre> Creating file .. finished, file size: 99 MB. Reading through funnel .. finished in 1593 ms. Reading through bottle .. finished in 7760 ms. </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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