473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
4 12027
<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TDH | last post by:
Hi, I need to do a raw copy of a byte array to an Int16 array. For e.g., I have an byte array which has length 100, and I need to copy this to an Int16 array that has length 50. So 2 elements...
4
by: xuatla | last post by:
Hi, How to copy a pointer to another pointer? Can I do in the following way: // START double *copyfrom = new double; double *copyto = new double;
2
by: plank | last post by:
Hey Peeps, Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The applet converts the file to Base64 and then POSTS the data to an...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
4
by: adnan boz | last post by:
Hi All, Does somebody know why File.Copy function is 10 times slower than standard windows drag and drop copy between folders, when I copy files over the network? Actually, I need to copy files...
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.