-1

I was wondering if there was any benefit/detriment to using output redirection on unix, compared to calling fprintf(file, ...);

edit: to further clarify. I'm writing a program that will need to do a data dump to disk: 50000+ lines by 40 characters of data.

The program itself takes up a significant amount of memory as it is, and I need to know whether or not allocating a buffer inside the program will incur a bigger memory penalty than using a unix output redirect.

I have noticed that the difference in output time is within margin of error. Both are very efficient and the only difference is that I need to write extra code for writing to a file without unix redirects.

However, most of my attempts at benchmarking have run up against the same issue: they don't show how much memory is allocated for the buffered output if I use output redirection.

1 Answers1

0

If there is any difference, it would stem from whether a chosen file descriptor is buffered or not. stderr is often unbuffered, stdout is buffered, so if you use output redirection for the latter, you might be able to notice a tiny, possibly but unlikely statistically significant speedup compared to writing to stderr.

The underlying mechanism for writes is the same in both cases, it is the initialization phase (opening and assigning file descriptors) that will be different, but it is only performed once.

Grigory Rechistov
  • 2,104
  • 16
  • 25