I do some numerical calculation in Java, C# and C++. Some of them save a lot of data (to the text file). What is the fastest way to do it?
C++.
ofstream file;
file.open(plik);
for(int i=0;i<251;i++){
for(int j=0;j<81;j++)
file<<(i-100)*0.01<<" "<<(j-40)*0.01<<" "<<U[i][j]<<endl;
file<<endl;
}
Which I assume is very fast ( am I right?:) )
Java
void SaveOutput(double[][] U, String fileName) throws IOException
{
PrintWriter tx = new PrintWriter(new FileWriter(fileName));
for(int i=0;i<251;i++)
{
for(int j=0;j<81;j++)
{
tx.println(String.format("%e %e %e ",(i - 100) * dz, (j - 40) * dz, U[i][j]));
}
tx.println();
}
tx.close();
}
The C# example is similar.
and here is what bothers me. I make a String object for each line (a lot of garbage). In this example it is not that much but sometimes I have 10 000 000 lines. This leads me to questions:
- Can the c++ example be faster?
- Should I use StringBuilder for Java or maybe it also bad due to number of lines
- There is any other way or library?
- What about C#?
Thank you