Re: make printing to file faster
On May 6, 3:06 pm, Vlad Ciubotariu <vcciu...@uwaterloo.ca> wrote:
I'm printing a list of arrays of int to a file. The performance of the
code below is pretty bad. Any suggestions on how to improve it?
thanks
vlad
try {
PrintStream out = new Pr=
intStream(inputFile);
out.println("p cnf " + num=
Vars + " " + numClauses);
for (Clause c : clauses) {=
for (int l=
iteral : c.getLiterals()) {
=
out.print(literal);
=
out.print(" ");
}
out.printl=
n(0);
}
for (Clause c : constraint=
s) {
for (int l=
iteral : c.getLiterals()) {
=
out.print(literal);
=
out.print(" ");
}
out.printl=
n(0);
}
out.close();
} catch (IOException e) {
throw new RuntimeException=
("Could not write clauses to file: " + e.getMessage());
}
And this is the performance for large repetitions:
1.6% - 16,459 ms - 1 inv. ca.uwaterloo.watform.ckt.sat.SatSolver.solve=
0.8% - 8,289 ms - 664,818 inv. java.io.PrintStream.print(int)
0.4% - 4,091 ms - 249,391 inv. java.io.PrintStream.println(int)
0.2% - 2,275 ms - 664,818 inv. java.io.PrintStream.print(java.lang.Str=
ing)
Instead of constantly doing out.print inside each loop, what if you
write to a StringBuffer and then write that to a file at the end?
Other way to improve performance would probably be to remove any
repeated or unnecessary clauses or constraints.