Note that there are some explanatory texts on larger screens.

plurals
  1. POfile IO performance C
    primarykey
    data
    text
    <p><BR> I have a question regarding file IO (C language) and its performance issues.</p> <p>I have an application that does a lot of file I/O (over its lifetime ~3-6hours, about 0.5-0.75TB, of mostly file output). At the moment my application <code>sprintf()</code>s everything into a char string and at the end of a line <code>write()</code>s, to a file_descriptor. My string is 1024 characters in length, but can vary anywhere from 64 to 1024. At any rate. </p> <p><strong>The question is:</strong> <BR> Would it make more sense to make a larger char string (say, 1MB?) and <code>sprintf()</code> everything into it before doing the <code>write()</code>? Or does it make more sense to skip <code>sprintf()</code> completely and simply <code>write()</code> directly to the file, assuming the buffering is taken care of by <code>write()</code>?</p> <p>Something I thought of, but unsure if it'll actually accomplish anything in terms of performance: <BR> What if I had a structure where I store the individual parts of the string, numbers and strings and do a mem_copy of the structure instead? I'm guessing similar to a binary write?</p> <p>I'm trying to achieve a "buffered" approach or anything that will maximize performance. The later is that I need to use that file for further processing. Any suggestions?</p> <p><strong>EDIT</strong> <br> I did some simple performance comparison with <code>printf(); + redir</code> and <code>sprintf(); write();</code> <BR> I'm simply copying ~20GB to a file.</p> <pre><code>char string[1024]; for(i=0;i&lt;(1&lt;&lt;20)*20;i++) printf("%s",string); ~/tmp/tests$ time ./printf.out &gt; testing real 2m22.101s user 0m28.214s sys 0m29.294s </code></pre> <p>as opposed to: <br></p> <pre><code>char string14[256]; ...etc for(i=0;1&lt;&lt;(1&lt;&lt;20)*20;i++){ sprintf(dst_string,"%s%s",dst_string, string14); sprintf(dst_string,"%s%s",dst_string, string24); sprintf(dst_string,"%s%s",dst_string, string34); sprintf(dst_string,"%s%s",dst_string, string44); write(fd, dst_string, 1024); } ~/tmp/tests$ time ./write.out real 1m48.206s user 0m58.544s sys 0m41.079s </code></pre> <p>The reason for multiple sprintf()s is to simulate copy->buffer and then write buffer. The time (real anyways) is not as insignificant as some comments may suggest. Granted this is a simple example and maybe in the scheme of computation + IO maybe it won't be.</p> <p>The thing that I'm a bit confused over the in the printf example, where did that extra time go? user+sys don't add up to real, shouldn't they at least be in the ballpark? Because there is a whole 1:30m missing.</p> <p>Does this test show any conclusions? sprintf + write > simply print+redir?</p> <p>Anyways, thank you all for the comments.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload