Velocity Reviews > Copy memory (array)

# Copy memory (array)

VijaKhara
Guest
Posts: n/a

 01-28-2008
Hi all,

I have a 2-D array with the size M x N. Now I need to segment this
array into two parts with sizes P x N and (M-P) x N.

P | M-P
|--------|------------------------|
| | |
| | |
| | | N
| | |
| | |
| | |
| | |
|--------|------------------------|
|
|<-- cutting line

If using the loop and copying point to point, I think it is not a fast
way especially when M and N are large. Is there any way in C to copy
the memory blocks so that it would be faster?

Thank you

Eric Sosman
Guest
Posts: n/a

 01-28-2008
VijaKhara wrote:
> Hi all,
>
> I have a 2-D array with the size M x N. Now I need to segment this
> array into two parts with sizes P x N and (M-P) x N.
>
> P | M-P
> |--------|------------------------|
> | | |
> | | |
> | | | N
> | | |
> | | |
> | | |
> | | |
> |--------|------------------------|
> |
> |<-- cutting line
>
> If using the loop and copying point to point, I think it is not a fast
> way especially when M and N are large. Is there any way in C to copy
> the memory blocks so that it would be faster?

First, re-examine why you need to perform this copying to
begin with. Is it possible to rearrange your program's data
structures so the copy becomes unnecessary? The fastest possible
copy is the one you don't perform.

If you must copy, I'd suggest using a loop over each of the
N rows, using memcpy() twice to copy the first P and the final
M-P elements of each row:

for (i = 0; i < N; ++i) {
memcpy (lhs[i], orig[i], P * sizeof orig[i][0]);
memcpy (rhs[i], orig[i]+P, (M-P) * sizeof orig[i][0]);
}

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

Ben Bacarisse
Guest
Posts: n/a

 01-28-2008
VijaKhara <(E-Mail Removed)> writes:

> Hi all,
>
> I have a 2-D array with the size M x N. Now I need to segment this
> array into two parts with sizes P x N and (M-P) x N.
>
> P | M-P
> |--------|------------------------|
> | | |
> | | |
> | | | N
> | | |
> | | |
> | | |
> | | |
> |--------|------------------------|
> |
> |<-- cutting line
>
> If using the loop and copying point to point, I think it is not a fast
> way especially when M and N are large. Is there any way in C to copy
> the memory blocks so that it would be faster?

You might find memcpy is faster than element copying, but then you
might also find it slower. The only way to know is to measure.

for (row = 0; row < N; row++) {
memcpy(left[row], big[row], P * sizeof big[0][0]);
memcpy(right[row], &big[row][P], (M - P) * sizeof big[0][0]);
}

[untested, un-compiled... unwise.]

--
Ben.

santosh
Guest
Posts: n/a

 01-28-2008
VijaKhara wrote:

> Hi all,
>
> I have a 2-D array with the size M x N. Now I need to segment this
> array into two parts with sizes P x N and (M-P) x N.
>
> P | M-P
> |--------|------------------------|
> | | |
> | | |
> | | | N
> | | |
> | | |
> | | |
> | | |
> |--------|------------------------|
> |
> |<-- cutting line
>
> If using the loop and copying point to point, I think it is not a fast
> way especially when M and N are large.

Have you actually measured the performance of hand-written loops and
found them not good enough, or are you just assuming that hand-written
loops will be slow?

> Is there any way in C to copy
> the memory blocks so that it would be faster?
>
> Thank you

In C the most generic Standard function to copy blocks of memory is
memcpy(). However if there is a possibility that the source and
destination will overlap then you must use memmove(). These routines
are likely to be optimised for your architecture. But in actual
practise you'll find that manual loops are "fast enough" for nearly all
purposes.

PS. Can you not merely treat the two segments of your array as separate
arrays? This is, if possible, likely to be the best solution as it will
involve no copying of data.

VijaKhara
Guest
Posts: n/a

 01-28-2008
Thank you, guys. It works very well. I need to segment it to 2 parts
because each part will be a parameter for a different module.

Thanks again.

SM Ryan
Guest
Posts: n/a

 01-29-2008
# If using the loop and copying point to point, I think it is not a fast
# way especially when M and N are large. Is there any way in C to copy
# the memory blocks so that it would be faster?

memcpy or memmove are likely as fast as anything else
possibly even faster, optimised for the machine and
special cased by the compiler. I'm not sure if you
have overlapping source and destination; if so use
memmove; otherwise you can use memcpy.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?

Willem
Guest
Posts: n/a

 01-29-2008
VijaKhara wrote:
) Thank you, guys. It works very well. I need to segment it to 2 parts
) because each part will be a parameter for a different module.

What's the module interface ?

If you write the module interface so that it takes an extra parameter
stating how many values to skip for each row. Then you don't need to
copy anything.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT