Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimum time to perform an operation: within, or after loop
    text
    copied!<p>I am reading a file to parse later on. The file is not likely to exceed an MB in size, so this is perhaps not a crucial question for me at this stage. But for best practise reasons, I'd like to know when is the optimum time to perform an operation.</p> <p>Example:</p> <p>Using a method I've pasted from <a href="http://www.dzone.com/snippets/java-read-file-string" rel="nofollow">http://www.dzone.com/snippets/java-read-file-string</a>, I am reading a buffer into a string. I would now like to remove all whitespace. My method is currently this:</p> <pre><code>private String listRaw; public boolean readList(String filePath) throws java.io.IOException { StringBuffer fileData = new StringBuffer(1024); BufferedReader reader = new BufferedReader( new FileReader(filePath)); char[] buf = new char[1024]; int numRead=0; while((numRead=reader.read(buf)) != -1){ String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); buf = new char[1024]; } reader.close(); listRaw = fileData.toString().replaceAll("\\s",""); return true; } </code></pre> <p>So, I remove all whitespace from the string at the time I store it - in it's entirety - to a class variable.</p> <p>To me, this means less processing but more memory usage. Would I be better off applying the <code>replaceAll()</code> operation on the <code>readData</code> variable as I append it to <code>fileData</code> for best practise reasons? Using more processing but avoiding passing superfluous whitespace around.</p> <p>I imagine this has little impact for a small file like the one I am working on, but what if it's a 200MB log file?</p> <p>Is it entirely case-dependant, or is there a consensus I'd do better to follow?</p> <hr> <p>Thanks for the input everybody. I'm sure you've helped to aim my mindset in the right direction for writing Java.</p> <p>I've updated my code to take into consideration the points raised. Including the suggestion by Don Roby that at some point, I may want to keep spaces. Hopefully things read better now!</p> <pre><code>private String listRaw; public boolean readList(String filePath) throws java.io.IOException { StringBuilder fileData = new StringBuilder(51200); BufferedReader reader = new BufferedReader(new FileReader(filePath)); char[] buf = new char[51200]; boolean spaced = false; while(reader.read(buf) != -1){ for(int i=0;i&lt;buf.length;i++) { char c = buf[i]; if (c != '\t' &amp;&amp; c != '\r' &amp;&amp; c != '\n') { if (c == ' ') { if (spaced) { continue; } spaced = true; } else { spaced = false; } fileData.append(c); } } } reader.close(); listRaw = fileData.toString().trim(); return true; } </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