Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of std::copy vs memcpy [duplicate]

How severe is the efficiency loss between using memcpy and std::copy?

I have a situation where the vector implementation on my system doesn't appear to use contiguous memory, which is making me have to std::copy its contents later on rather than doing memcpy(dest, &vec[0], size);. I'm not sure how badly this is likely to impact efficiency.

like image 613
John Humphreys Avatar asked Sep 02 '11 15:09

John Humphreys


1 Answers

A reasonably decent implementation will have std::copy compile to a call memmove in the situations where this is possible (i.e. the element type is a POD).

If your implementation doesn't have contiguous storage (the C++03 standard requires it), memmove might be faster than std::copy, but probably not too much. I would start worrying only when you have measurements to show it is indeed an issue.

like image 160
R. Martinho Fernandes Avatar answered Nov 07 '22 20:11

R. Martinho Fernandes