Question

I want know what the fastest function for copy n bytes from *source to *destination, when *destination = *source + k, and k is netural or zero.

The memcpy() function has undefind behavior for overlapping.

The memmove() function is not the best, since it has to check the overlapping side.

There is another function that is optimal to my case?

Thanks in advance!

Was it helpful?

Solution 2

memmove() has to spend some time determining how and whether the source and target overlap, so it can decide the order in which to copy the data.

Hypothetically, a version of memmove() that omits this initial calculation, either because it assumes a particular direction or because it has an extra parameter that lets you tell it, might be a little faster than memmove().

But there is no such function in the standard library.

You could write your own function, but it's unlikely that it would be as fast as your system's memmove(), which is very likely to be heavily optimized. You could grab a copy of the source code for your system's memmove() function (if it's available) and modify it, but the result would likely be non-portable (memmove() can depend on the characteristics of the system it's running on).

But it's very unlikely that it would be worth the effort. The time memmove() spends doing the initial calculation is, for moves of reasonable size, likely to be a small fraction of the time spent copying the data.

Unless you know from actual measurement that the initial computation performed by memmove() carries a significant performance penalty for your program, just use memmove() itself.

OTHER TIPS

memmove() is fine. It will figure out the direction of the move on its own by comparing the two pointers — an implementation might look something like:

void * memmove(void *dst, const void *src, size_t len)
{
    intptr_t dsti = dst;
    intptr_t srci = src;
    if (src > dst) {
        return _memmove_up(dst, src, len);
    } else {
        return _memmove_down(dst, src, len);
    }
 }

On most CPUs, this will compile to perhaps a half dozen extra instructions, which are only executed once when the function is called — there is no benefit to bypassing this.

(In theory, there is no reason why memcpy() should not be able to do a similar comparison itself to remove the distinction between it and memmove(). However, for some unfortunate historical reason, the authors of the C standard library did not decide to make this simplification, so we're stuck with these two functions.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top