473,668 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 12054
<st************ *@hotmail.com> wrote in message
news:e7******** *************** **@posting.goog le.com...
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*si zeof(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
2716
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 in the byte array gets copied to 1 element in the Int16 array. In VB6, this was easily done using CopyMemory. I need something in .NET that works as fast as CopyMemory. I
4
2244
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
8317
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 ASP page. The ASP code I have is: <% Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
7
7403
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 example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
4
3484
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 from one computer to another over a 1GBit ethernet network as fast as I can. Although windows copy works at %40 performance, File.Copy could not go further than %4. What could be the reason? What would be the best solution; is it feasible to
10
3000
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: #include <stdlib.h> #ifndef __LIST_H__ #define __LIST_H__
35
2828
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 easier without much of overhead. Then why not reference ? Thanks /P
15
2569
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 determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
0
5558
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
8382
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.