开发者

copy smaller array into larger array

开发者 https://www.devze.com 2023-01-16 14:48 出处:网络
I have two arrays of chars, allocated as follo开发者_开发知识库ws: unsigned char *arr1 = (unsigned char *)malloc((1024*1024) * sizeof(char));

I have two arrays of chars, allocated as follo开发者_开发知识库ws:

 unsigned char *arr1 = (unsigned char *)malloc((1024*1024) * sizeof(char));
 unsigned char *arr2 = (unsigned char *)malloc((768*768) * sizeof(char));

I would like to copy arr2 into arr1, but preserve the row/column structure. This means that only the first 768 bytes of each of the first 768 rows will be changed in arr1.

I wrote a for loop for this, but it's not fast enough for my needs.

for (int x = 0; x < 768; x++) //copy each row
{
    memcpy(arr1+(1024*x),arr2+(768*x), nc);
}

Is there a better solution?


maybe get rid of the multiplications

size_t bigindex = 0, smallindex = 0;
for (int x = 0; x < 768; x++) //copy each row
{
    memcpy(arr1 + bigindex, arr2 + smallindex, nc);
    bigindex += 1024;
    smallindex += 768;
}

Edit d'oh! use the pointers!

unsigned char *a1 = arr1;
unsigned char *a2 = arr2;
for (int x = 0; x < 768; x++) //copy each row
{
    memcpy(a1, a2, nc);
    a1 += 1024;
    a2 += 768;
}


Rather than copying the whole contents, perhaps initially allocate that as 768 separate arrays, and then use realloc to extend them instead of copying?

Not sure if it really saves any time in the end to have so many separate calls to malloc() rather that the move loop. But if you are having to copy many times, it might. It also assumes you don't want to further modify the original...


What do you mean by not fast enough? Did you benchmark this?

I suppose your nc is just a named constant that stands for 768?

There is not much that will go faster if you use builtin memcpy that your compiler provides. The problems that might be existing in your approach:

  • alignment issues
  • your constant nc not being a compile time constant
  • you don't have the correct compiler flags
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号