Copy memory (array)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VijaKhara

    Copy memory (array)

    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

    #2
    Re: Copy memory (array)

    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
    esosman@ieee-dot-org.invalid

    Comment

    • Ben Bacarisse

      #3
      Re: Copy memory (array)

      VijaKhara <VijaKhara@gmai l.comwrites:
      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.

      Comment

      • VijaKhara

        #4
        Re: Copy memory (array)

        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.

        Comment

        • SM Ryan

          #5
          Re: Copy memory (array)

          # 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?

          Comment

          • Willem

            #6
            Re: Copy memory (array)

            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

            Comment

            Working...