Copy memory (array)

Discussion in 'C Programming' started by VijaKhara, Jan 28, 2008.

  1. VijaKhara

    VijaKhara Guest

    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
    VijaKhara, Jan 28, 2008
    #1
    1. Advertising

  2. VijaKhara

    Eric Sosman Guest

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

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 28, 2008
    #2
    1. Advertising

  3. VijaKhara <> 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.
    Ben Bacarisse, Jan 28, 2008
    #3
  4. VijaKhara

    santosh Guest

    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.
    santosh, Jan 28, 2008
    #4
  5. VijaKhara

    VijaKhara Guest

    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.
    VijaKhara, Jan 28, 2008
    #5
  6. VijaKhara

    SM Ryan Guest

    # 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?
    SM Ryan, Jan 29, 2008
    #6
  7. VijaKhara

    Willem Guest

    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
    Willem, Jan 29, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Venkat
    Replies:
    4
    Views:
    969
    Venkat
    Dec 5, 2003
  2. Replies:
    4
    Views:
    13,406
    Old Wolf
    Aug 13, 2004
  3. Alex
    Replies:
    2
    Views:
    1,218
  4. Replies:
    26
    Views:
    2,106
    Roland Pibinger
    Sep 1, 2006
  5. Developer
    Replies:
    4
    Views:
    709
    Daniel Pitts
    Feb 25, 2009
Loading...

Share This Page