Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Supposedly, as Nawaz said, the assignment version <em>should</em> be faster on most platform. That's because <code>memcpy()</code> will copy byte by byte while the second version could copy 4 bytes at a time.</p> <p>As it's always the case, you should always profile applications to be sure that what you expect to be the bottleneck matches the reality.</p> <p><strong>Edit</strong><br> Same applies to dynamic array. Since you mention C++ you should use <code>std::copy()</code> algorithm in that case.</p> <p><strong>Edit</strong><br> This is code output for Windows XP with GCC 4.5.0, compiled with -O3 flag:</p> <pre><code>extern "C" void cpy(float* d, float* s, size_t n) { memcpy(d, s, sizeof(float)*n); } </code></pre> <p>I have done this function because OP specified dynamic arrays too. </p> <p>Output assembly is the following:</p> <pre><code>_cpy: LFB393: pushl %ebp LCFI0: movl %esp, %ebp LCFI1: pushl %edi LCFI2: pushl %esi LCFI3: movl 8(%ebp), %eax movl 12(%ebp), %esi movl 16(%ebp), %ecx sall $2, %ecx movl %eax, %edi rep movsb popl %esi LCFI4: popl %edi LCFI5: leave LCFI6: ret </code></pre> <p>of course, I assume all of the experts here knows what <code>rep movsb</code> means.</p> <p>This is the assignment version:</p> <pre><code>extern "C" void cpy2(float* d, float* s, size_t n) { while (n &gt; 0) { d[n] = s[n]; n--; } } </code></pre> <p>which yields the following code:</p> <pre><code>_cpy2: LFB394: pushl %ebp LCFI7: movl %esp, %ebp LCFI8: pushl %ebx LCFI9: movl 8(%ebp), %ebx movl 12(%ebp), %ecx movl 16(%ebp), %eax testl %eax, %eax je L2 .p2align 2,,3 L5: movl (%ecx,%eax,4), %edx movl %edx, (%ebx,%eax,4) decl %eax jne L5 L2: popl %ebx LCFI10: leave LCFI11: ret </code></pre> <p>Which moves 4 bytes at a time.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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