First let’s understand what the problem is. Memcpy copies memory one byte at a time. Which is fine as long as you don’t have a lot of memory to copy. If you find yourself needing to copy large chunks of memory from one spot to the next you may very well find yourself loosing a lot of process time in the memcpy routine. One possible way to optimize this call is to define a copy routine of your own that is optimized for the data types you need to copy. For example, let’s say that you have data that will always be in 8 byte sections. A good way to optimize this would be to copy the data 8 bytes at a time versus 1 byte at a time via memcpy. Consider the following code:

void uint8copy(void *dest, void *src, size_t n){
    uint64_t * ss = (uint64_t)src;
    uint64_t * dd = (uint64_t)dest;
    n = n * sizeof(uint8_t)/sizeof(uint64_t); 

    while(n--)
        *dd++ = *ss++;
}//end uint8copy()

The code is pretty simple, it takes in a dest, src, and n (size) the same as memcpy. But, it converts the src and dest pointers to uint64_t or 64 bit pointers before making the copy. Next it determines how many times to iterate and begins copying data 64 bits or 8 bytes at a time. This approach works well for this particular scenario and may assist in being a starting point for your code as well.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *