C++: IO performance issue
<p>I have an large array in memory. I am writing this in file using:</p>

<pre><code>             FILE* fp = fopen("filename", "wb");
             fwrite(array, sizeof(uint32_t), 1500000000 , fp); // array saved
             fflush(fp) ;
             fclose(fp);
</code></pre>

<p>and reading it again using:</p>

<pre><code>              FILE* fp = fopen("filename", "rb");
              fread(array, sizeof(uint32_t), 1500000000 , fp);
              fclose(fp);
</code></pre>

<p>For, writing it takes 7 sec and for reading it takes 5 sec.</p>

<p>Actually, I have not to write whole array. I have to write and read it by checking some conditions. Like (example case):</p>

<pre><code>#include&lt;iostream&gt;
#include &lt;stdint.h&gt;
#include &lt;cstdio&gt;
#include &lt;cstdlib&gt;
#include &lt;sstream&gt;

using namespace std;

main()
{
      uint32_t* ele = new uint32_t [100] ;
      for(int i = 0; i &lt; 100 ; i++ )
      ele[i] = i ;

      for(int i = 0; i &lt; 100 ; i++ ){
          if(ele[i] &lt; 20)
          continue ;
          else
          // write  ele[i] to file
          ;   
      }

 for(int i = 0; i &lt; 100 ; i++ ){
          if(ele[i] &lt; 20)
          continue ;
          else
          // read  number from file
          // ele[i] = number * 10 ;
          ;   
      }

     std::cin.get();
}
</code></pre>

<p>For this reason what I am doing is:</p>

<p>writing using:  </p>

<pre><code>for(int i = 0; i &lt; 1500000000 ; i++ ){
if (arrays[i] &lt; 10000000)
continue ;
uint32_t number = arrays[i] ;
fwrite(&amp;number, sizeof(uint32_t), 1, fp1);
}
</code></pre>

<p>And reading using: <code>fread(&amp;number, sizeof(uint32_t), 1, fp1);</code></p>

<p>This case: writing takes 2.13 min and for reading it takes 1.05 min.</p>

<p>Which is quite long time for me. Can anybody help me, why is this happening (in second case file size is less than first one) ? And How to solve this issue ? Any other better approach ?</p>
