Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to fill 100 (or even 1000) <code>unsigned int</code> elements, then you can choose any method you want, be it <code>std::fill_n</code>, or <code>for</code> loop - the number is so small you won't see the difference, even if you do this operation very often.</p> <p>However, if you want to set values for a bigger array, say, 8k x 8k texture with pixels composed of 4 unsigned color components, then there is a short comparison of the methods you can use:</p> <pre><code>#include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;cstdint&gt; int main(){ long unsigned const size = 8192 * 8192 * 4; unsigned* arr = new unsigned[size]; clock_t t1 = clock(); memset(arr, 0, size*sizeof(unsigned)); clock_t t2 = clock(); std::fill_n(arr, size, 123); clock_t t3 = clock(); for(int i = 0; i &lt; size; ++i) *(arr + i) = 123; clock_t t4 = clock(); int64_t val = 123; val = val &lt;&lt; 32 | 132; for(int i = 0; i &lt; size / 2; ++i) *(int64_t*)(arr + i * 2) = val; clock_t t5 = clock(); std::cout &lt;&lt; "memset = " &lt;&lt; t2 - t1 &lt;&lt; std::endl; std::cout &lt;&lt; "std::fill_n = " &lt;&lt; t3 - t2 &lt;&lt; std::endl; std::cout &lt;&lt; "for 32 = " &lt;&lt; t4 - t3 &lt;&lt; std::endl; std::cout &lt;&lt; "for 64 = " &lt;&lt; t5 - t4 &lt;&lt; std::endl; delete arr; return 0; } </code></pre> <p><strong>1. <code>memset</code></strong></p> <p>This function is used here only to show you how fast <em>zeroing</em> your array could be, in comparison to other methods. It's the fastest solution, but only usable when you want to set every byte to the same value (especially useful with <code>0</code> and <code>0xFF</code> in your case, I guess).</p> <p><strong>2. <code>std::fill_n</code> and <code>for</code> loop with 32-bit value</strong></p> <p><code>std::fill_n</code> looks to be the slowest of the solutions, and it is even slightly <strong>slower</strong> than the <code>for</code> solution with 32-bit values.</p> <p><strong>3. <code>for</code> loop with 64-bit value ON 64-bit SYSTEM</strong></p> <p>I guess this is the solution you could go for, since it wins this competition. However, if your machine were 32-bit, then I would expect the results to be comparable to the loop with 32-bit values (depends on the compiler and processor), since processor will handle one 64-bit value as two 32-bit values.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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