Re: fprintf performance by Doug
Doug
Tue May 29 00:34:41 CDT 2007
On 28 May 2007 22:21:40 -0700, Ezmeralda <ezmeralda@gmx.de> wrote:
>
>Hallo,
>
>I am using fprintf to write a very large byte-array to disk.
>In order to do this, I am writing each byte with a separate
>fprintf-call in a text file (text file is a hard requirement
>for my application).
>
>Unfortunately, the performance of this disk-write is very poor:
>over all, I do get only ~17Mbit/s, which is not acceptable
>for my purpose.
>
>How can I speed up my application?
Buffer your data and write the largest blocks possible. The stdio routines
buffer by default, but writing a byte at a time is still a lot slower than
writing large blocks, especially when using the multithreaded runtime,
which acquires and releases a CRITICAL_SECTION on every call.[*] And for
writing single bytes, fprintf is a lot slower than, say, fputc. If you need
to format the data, you're really not "writing a very large byte-array to
disk", but if in fact you really are writing an array of bytes, you should
be able to save all your data with a single call to fwrite. That will give
you the best performance you will find in stdio.
[*] VC2005 provides "unlocked" versions which do not perform the locking
done by the standard functions.
--
Doug Harrison
Visual C++ MVP