Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance Cost of a Memcopy in C/C++

Tags:

So whenever I write code I always think about the performance implications. I've often wondered, what is the "cost" of using a memcopy relative to other functions in terms of performance?

For example, I may be writing a sequence of numbers to a static buffer and concentrate on a frame within the buffer, in order to keep the frame once I get to the end of the buffer, I might memcopy all of it to the beginning OR I can implement an algorithm to amortize the computation.

like image 416
Cenoc Avatar asked Jun 13 '10 15:06

Cenoc


People also ask

Why is memcpy so slow?

I also had some code that I really needed to speed up, and memcpy is slow because it has too many unnecessary checks. For example, it checks to see if the destination and source memory blocks overlap and if it should start copying from the back of the block rather than the front.

What can I use instead of memcpy?

memmove() is similar to memcpy() as it also copies data from a source to destination.

Why memcpy is used?

C # in Telugu The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string. h” header file in C language.


2 Answers

memcpy is generally optimized to maximize memory bandwidth of large copies. Of course, it's not as fast as avoiding a copy completely, and for short copies of fixed size, direct assignment may be faster since memcpy has extra code to deal with odd lengths.

But when you need to copy a block of memory, it's hard to beat memcpy. It's highly portable and most compilers go to great lengths to make it fast, whether that's using SIMD instructions or maybe inlining.

like image 89
Ben Voigt Avatar answered Oct 02 '22 06:10

Ben Voigt


It's ok to consider performance implications, but don't become too distracted from the real goal of writing good clean code. If you are inclined to obsess about performance even when you know better, try to focus on higher level implications, and ignore the bit-by-bit stuff such as memcpy, which you can trust the compiler and library authors to optimize.

Generally avoid premature optimization of this low-level kind because it consumes your time, the effects bubble up to infect the entire program, and without measurements, you cannot expect to achieve any performance gains.

like image 42
John Avatar answered Oct 02 '22 07:10

John