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

前端 未结 6 1747
名媛妹妹
名媛妹妹 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:30

    That is not the results I get:

    > g++ -O3 XX.cpp 
    > ./a.out
    cast:      5 ms
    memcpy:    4 ms
    std::copy: 3 ms
    (counter:  1264720400)
    
    Hardware: 2GHz Intel Core i7
    Memory:   8G 1333 MHz DDR3
    OS:       Max OS X 10.7.5
    Compiler: i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1
    

    On a Linux box I get different results:

    > g++ -std=c++0x -O3 XX.cpp 
    > ./a.out 
    cast:      3 ms
    memcpy:    4 ms
    std::copy: 21 ms
    (counter:  731359744)
    
    
    Hardware:  Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
    Memory:    61363780 kB
    OS:        Linux ip-10-58-154-83 3.2.0-29-virtual #46-Ubuntu SMP
    Compiler:  g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    

提交回复
热议问题