views:

136

answers:

3

I have two arrays of chars, allocated as follows:

 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?

Thanks!

+3  A: 

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;
}
pmg
Thanks, this worked! I didn't realize that multiplication would slow it down.
Nick Lee
A: 

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
Jens Gustedt
+1  A: 

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...

Kendall Helmstetter Gelner
+1 Nice side tracking :) ... but `realloc` will probably have to copy data anyway
pmg
There's a good possibility, but even avoiding some copies might be enough to improve performance. And, it probably would have avoided the expensive multiplications that were the real issue...
Kendall Helmstetter Gelner