Why is `std::copy` 5x (!) slower than `memcpy` for reading one int from a char buffer, in my test program?

前端 未结 6 1741
名媛妹妹
名媛妹妹 2021-02-05 14:31

This is a follow-up to this question where I posted this program:

#include 
#include 
#include 
#include 

        
6条回答
  •  离开以前
    2021-02-05 15:15

    According to assembler output of G++ 4.8.1, test_memcpy:

    movl    (%r15), %r15d
    

    test_std_copy:

    movl    $4, %edx
    movq    %r15, %rsi
    leaq    16(%rsp), %rdi
    call    memcpy
    

    As you can see, std::copy successfully recognized that it can copy data with memcpy, but for some reason further inlining did not happen - so that is the reason of performance difference.

    By the way, Clang 3.4 produces identical code for both cases:

    movl    (%r14,%rbx), %ebp
    

提交回复
热议问题