Copy memory (array)

V

VijaKhara

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
 
E

Eric Sosman

VijaKhara said:
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, orig, P * sizeof orig[0]);
memcpy (rhs, orig+P, (M-P) * sizeof orig[0]);
}
 
B

Ben Bacarisse

VijaKhara said:
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.]
 
S

santosh

VijaKhara said:
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.
 
V

VijaKhara

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

SM Ryan

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

Willem

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top