By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

[help] How to copy fast an array to another array?

P: n/a
Hi!

I would appreciate some help with this (basic) question. I need to
copy an 2D array x to another 2D array y. I know how to do it copying
element by element, but I would like to use a faster method (because I
have to do many many copies). Can anyone tell me a faster method (one
who thak's less time running)? Thanks.

I'm using the following method:

----------------------------------------

int
x[3][3] = {{0,1,2},{3,4,5},{6,7,8}},
y[3][3],
i, j;

int main()
{
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
y[i][j] = x[i][j];

return 0;
}

----------------------------------------
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<st*************@hotmail.com> wrote in message
news:e7*************************@posting.google.co m...
Hi!

I would appreciate some help with this (basic) question. I need to
copy an 2D array x to another 2D array y. I know how to do it copying
element by element, but I would like to use a faster method (because I
have to do many many copies). Can anyone tell me a faster method (one
who thak's less time running)? Thanks.

I'm using the following method:

----------------------------------------

int
x[3][3] = {{0,1,2},{3,4,5},{6,7,8}},
y[3][3],
i, j;

int main()
{
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
y[i][j] = x[i][j];

return 0;
}

----------------------------------------


You could use memcpy():

memcpy(y,x,9*sizeof(int));

But if your arrays contain only 9 elements total, I doubt that change will
have any noticeable impact on overall performance. It could even be slower,
for all I know. You might want to run a few tests before you adopt it as
the preferred method for copying arrays. It's always best to optimize based
upon measurements (e.g. information from a profiler) than to optimize based
on a guess as to what makes you application slow, because programmers tend
to make bad guesses about performance.

--
David Hilsee
Jul 22 '05 #2

P: n/a
st*************@hotmail.com wrote:
Hi!

I would appreciate some help with this (basic) question. I need to
copy an 2D array x to another 2D array y. I know how to do it copying
element by element, but I would like to use a faster method (because I
have to do many many copies). Can anyone tell me a faster method (one
who thak's less time running)? Thanks.

I'm using the following method:

----------------------------------------

int
x[3][3] = {{0,1,2},{3,4,5},{6,7,8}},
y[3][3],
i, j;

int main()
{
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
y[i][j] = x[i][j];

return 0;
}

----------------------------------------


Optimization is system dependant so to get the best possible answer, you
need to tune it to your system. Here are all the alternatives, you
choose the one which runs fastest on your system.
template <typename T, int Rows, int Cols>
void MatrixCopy(
T (&Dest)[Rows][Cols],
const T (&Src)[Rows][Cols]
) {
T * Destp = & Dest[0][0];
const T * Srcp = & Src[0][0];

const int count = Rows * Cols;

for ( int i = 0; i < count; ++ i )
{
* ( Destp ++ ) = * (Srcp ++ );
}
}

Or
template <typename T, int Rows, int Cols>
void MatrixCopy(
T (&Dest)[Rows][Cols],
const T (&Src)[Rows][Cols]
) {
T * Destp = & Dest[0][0];
const T * Srcp = & Src[0][0];

const int count = Rows * Cols;

for ( int i = 0; i < count; ++ i )
{
Destp[ i ] = Srcp[ i ];
}
}

or
template <typename T, int Rows, int Cols>
void MatrixCopy(
T (&Dest)[Rows][Cols],
const T (&Src)[Rows][Cols]
) {
T * Destp = & Dest[0][0];
const T * Srcp = & Src[0][0];

const int count = Rows * Cols;

memcpy(
static_cast< void * >( Destp ),
static_cast< const void * >( Src ),
sizeof( Dest )
);

}

or

template <typename T>
void MatrixCopy(
T (&Dest)[3][3],
const T (&Src)[3][3]
) {
T * Destp = & Dest[0][0];
const T * Srcp = & Src[0][0];

register T v0 = Src[0][0];
register T v1 = Src[0][1];
register T v2 = Src[0][2];
register T v3 = Src[1][0];
register T v4 = Src[1][1];
register T v5 = Src[1][2];
register T v6 = Src[2][0];
register T v7 = Src[2][1];
register T v8 = Src[2][2];

Dest[0][0] = v0;
Dest[0][1] = v1;
Dest[0][2] = v2;
Dest[1][0] = v3;
Dest[1][1] = v4;
Dest[1][2] = v5;
Dest[2][0] = v6;
Dest[2][1] = v7;
Dest[2][2] = v8;

}

Jul 22 '05 #3

P: n/a
struct Array3x3
{
int data[3][3];
};

CastToArray3x3(int input[3][3])
{
return reinterpr...
}

int main()
{
Array3x3 a;

a = ...;

//Time goes by

Array3x3 b;
//Here comes the beautiful part:

b = a;
}
Jul 22 '05 #4

P: n/a
st*************@hotmail.com wrote:
Hi!

I would appreciate some help with this (basic) question. I need to
copy an 2D array x to another 2D array y. I know how to do it copying
element by element, but I would like to use a faster method (because I
have to do many many copies).

#include <cstring>
int
x[3][3] = {{0,1,2},{3,4,5},{6,7,8}},
y[3][3],
i, j;

int main()
{


std::memcpy(&y, &x, sizeof y);
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.