Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The fastest way to save large data to a file

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:

  1. Can the c++ example be faster?
  2. Should I use StringBuilder for Java or maybe it also bad due to number of lines
  3. There is any other way or library?
  4. What about C#?

Thank you

like image 938
Lukasz Madon Avatar asked Nov 04 '10 23:11

Lukasz Madon


People also ask

What is the fastest way to transfer large files online?

TransferNow is the simplest, fastest and safest interface to transfer and share files. Send photos, videos and other large files without a manditory subscription thanks to TransferNow. Create a TransferNow account to get higher transfer capacity and additional features. It is simple and secure!

What is the fastest way to transfer a file?

The fastest way to send documents is an online file transfer service. WeTransfer and MailBigFile are two services that allow to transfer big files online for free. The size limit is 2GB and they both allow to send the file directly to your recipient's inbox.

How can I send 20 GB for free?

MyAirBridge. With MyAirBridge(Opens in a new window), you can upload a file and email a link to a specific recipient or just upload the file and generate a link to share with anyone. You can send a file as large as 20GB for free.


1 Answers

Profile it. Run the code, time it, see how long it takes. If the amount of time it takes is acceptable, use it. If not, figure out what piece is taking a long amount of time to run, and optimize it.

  • Make it right.
  • Make it fast.

That order. (Some people add "make it run/build" before those two...)

That said, I've actually run metrics on this sort of thing before. The short of it: You're waiting for disk, and disk is ungodly slow. It doesn't matter if you're writing in C or C++, or Java, they're all waiting for the hard disk.

Here's a previous post that I did on various I/O methods in C. Not exactly what you're looking for, but might be informative.

like image 64
Thanatos Avatar answered Oct 03 '22 07:10

Thanatos