views:

123

answers:

3

I'm making a program that process a big file and output something to another that I need to use later. I'm wondering should I just print the output and redirect that to a file, or should I just write to the file in the program. Since this will be a very big file, I would like to know which way is faster, every bit counts.

+1  A: 

You question is really, "Should I write to stdout, or use native file i/o".

The answer will depend somewhat on how you process the file (can it be processed and output line by line), and how optimally your file i/o code is written.

Its quite possible to write code that outputs directly to a file that is slower than code that writes to stdout.

sylvanaar
+1  A: 

What's the difference? Stdout is a stream, so is a file. On most operating systems, there is literally no difference. On windows, there are different functions you have to use when handling file streams vs output streams, but they're still almost exactly the same API (just the file ones are prefixed with 'f'). I'd be very surprised if there's a difference in performance.

You can of course use alternative APIs for files, but I do not see a compelling reason to do so, since the files are still streams at the OS level.

rmeador
+1  A: 

If the typical output is a text format, i would prefer stdout. You can simply check if on the terminal, redirect to a file or pipe it to the next command. The performance should be the same. For binary output the file output is more typical.

Arne Burmeister