Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is <strong>very</strong> inefficient. You re-read the file second time on every line(!!!) in the file. Disk IO is very slow.</p> <p>What you should do is to load the file into a parsed memory structure (an array of doubles), and then do a nested loop over it.</p> <blockquote> <p>Am I correct in thinking my code is not able to be multithreaded?</p> </blockquote> <p>You're incorrect. This task would benefit a lot from threading. But your first priority is to get rid of repetitive IO. I'd guess the performance would be good enough then. </p> <p><strong>UPDATE</strong> to <strong>UPDATE</strong></p> <p>Rewrote your class to multiple threads (4 by default). Downside: lines in the output file are not written in order, though by using unix sort utility you may sort it after the computation, if needed. Both A->B and B->A are still calculated as I couldn't come up with a simple way to store the result of A->B short of using Java 64bit and installing some 64G of RAM.</p> <pre><code>import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Coord { public int a, b, c, d, e, f; private static class CoordsThread extends Thread { private int start; private int end; private List&lt;Coord&gt; coords; private PrintWriter out; public CoordsThread(int start, int end, List&lt;Coord&gt; list, PrintWriter out) { this.start = start; this.end = end; this.coords = list; this.out = out; // last block can be shorter if( this.end &gt; this.coords.size() ) this.end = this.coords.size(); } public void run() { System.out.println("started thread "+getName()+" for ["+start+";"+end+")"); for (int i = start; i &lt; end; i++) { for (int j = 0; j &lt; coords.size(); j++ ) { Coord c1 = coords.get(i); Coord c2 = coords.get(j); double foo = ((c1.a - c2.a) * (c1.a - c2.a)) * 1; double goo = ((c1.b - c2.b) * (c1.b - c2.b)) * 1; double hoo = ((c1.c - c2.c) * (c1.c - c2.c)) * 2; double joo = ((c1.d - c2.d) * (c1.d - c2.d)) * 2; double koo = ((c1.e - c2.e) * (c1.e - c2.e)) * 4; double loo = ((c1.f - c2.f) * (c1.f - c2.f)) * 4; double zoo = Math.sqrt(foo + goo + hoo + joo + koo + loo); synchronized (out) { out.println(i*coords.size()+j + "; " + i + " " + j + " " + zoo); } } } System.out.println("completed thread "+getName()); } } public static void main(String[] args) throws Exception { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("2.txt"))); Scanner sc = new Scanner(new File("1.txt")); List&lt;Coord&gt; coords = new ArrayList&lt;Coord&gt;(); // for each line in the file while (sc.hasNextLine()) { String[] numstrs = sc.nextLine().split("\\s+"); Coord c = new Coord(); c.a = Integer.parseInt(numstrs[1]); c.b = Integer.parseInt(numstrs[2]); c.c = Integer.parseInt(numstrs[3]); c.d = Integer.parseInt(numstrs[4]); c.e = Integer.parseInt(numstrs[5]); c.f = Integer.parseInt(numstrs[6]); coords.add(c); } System.out.println("total lines read: "+coords.size()); int threadsCount = 4; List&lt;Thread&gt; ths = new ArrayList&lt;Thread&gt;(); int blockSize = coords.size()/threadsCount+1; for( int i=0; i&lt;threadsCount; ++i ) { CoordsThread ct = new CoordsThread(i*blockSize, (i+1)*blockSize, coords, out); ct.setName("Block"+i); ths.add(ct); } for (Thread th : ths) { th.start(); } for (Thread th : ths) { th.join(); } out.flush(); out.close(); } } </code></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