Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All compilers I know will replace a simple <code>std::copy</code> with a <code>memcpy</code> when it is appropriate, or even better, vectorize the copy so that it would be even faster than a <code>memcpy</code>.</p> <p>In any case: profile and find out yourself. Different compilers will do different things, and it's quite possible it won't do exactly what you ask.</p> <p>See <a href="http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf" rel="noreferrer">this presentation on compiler optimisations</a> (pdf).</p> <p>Here's <a href="http://gcc.godbolt.org/#%7B%22version%22%3A3%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%2C%22commentOnly%22%3Atrue%7D%2C%22compilers%22%3A%5B%7B%22source%22%3A%22%23include%20%3Calgorithm%3E%5Cn%5Cnstruct%20foo%5Cn%7B%5Cn%20%20int%20x%2C%20y%3B%20%20%20%20%5Cn%7D%3B%5Cn%5Cnvoid%20bar(foo*%20a%2C%20foo*%20b%2C%20size_t%20n)%5Cn%7B%5Cn%20%20std%3A%3Acopy(a%2C%20a%20%2B%20n%2C%20b)%3B%5Cn%7D%5Cn%22%2C%22compiler%22%3A%22%2Fusr%2Fbin%2Fg%2B%2B-4.7%22%2C%22options%22%3A%22-O%20-march%3Dnative%22%7D%5D%7D" rel="noreferrer">what GCC does</a> for a simple <code>std::copy</code> of a POD type.</p> <pre><code>#include &lt;algorithm&gt; struct foo { int x, y; }; void bar(foo* a, foo* b, size_t n) { std::copy(a, a + n, b); } </code></pre> <p>Here's the disassembly (with only <code>-O</code> optimisation), showing the call to <code>memmove</code>:</p> <pre><code>bar(foo*, foo*, unsigned long): salq $3, %rdx sarq $3, %rdx testq %rdx, %rdx je .L5 subq $8, %rsp movq %rsi, %rax salq $3, %rdx movq %rdi, %rsi movq %rax, %rdi call memmove addq $8, %rsp .L5: rep ret </code></pre> <p>If you change the function signature to</p> <pre><code>void bar(foo* __restrict a, foo* __restrict b, size_t n) </code></pre> <p>then the <code>memmove</code> becomes a <code>memcpy</code> for a slight performance improvement. Note that <code>memcpy</code> itself will be heavily vectorised.</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