473,395 Members | 1,949 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,395 software developers and data experts.

fast array copy c++

Dear developers,
I have a question. Is there a faster array copy than a for loop. What
would you suggest for this code sample?

void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}

Thank you for your answers.
Jun 27 '08 #1
7 3603
On 2008-05-21 08:46:29 -0400, fr33host <fr******@gmail.comsaid:
Dear developers,
I have a question. Is there a faster array copy than a for loop. What
would you suggest for this code sample?

void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}

Thank you for your answers.
memcpy.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2
On Wed, 21 May 2008 05:46:29 -0700, fr33host wrote:
Dear developers,
I have a question. Is there a faster array copy than a for loop. What
would you suggest for this code sample?

void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}

Thank you for int main()
An option:

std::string source("COPY THIS STRING WITH STL COPY");
std::string dest;
copy(source.begin(),source.end(),back_inserter(des t));
std::cout << dest << std::endl;
return 0;

--
Umut
Jun 27 '08 #3
utab wrote:
On Wed, 21 May 2008 05:46:29 -0700, fr33host wrote:
>Dear developers,
I have a question. Is there a faster array copy than a for loop. What
would you suggest for this code sample?

void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}

Thank you for int main()

An option:

std::string source("COPY THIS STRING WITH STL COPY");
std::string dest;
copy(source.begin(),source.end(),back_inserter(des t));
std::cout << dest << std::endl;
return 0;
this is slower because it doesn't take advantage of knowing the size of
the destination in advance.

An STL implementation of the for cycle is:

std::fill(src, src + recordLen, dest);

Best wishes,

Zeppe
Jun 27 '08 #4
On Wed, 21 May 2008 14:05:28 +0100, Zeppe wrote:
utab wrote:
>On Wed, 21 May 2008 05:46:29 -0700, fr33host wrote:
>>Dear developers,
I have a question. Is there a faster array copy than a for loop. What
would you suggest for this code sample?

void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}

Thank you for int main()

An option:

std::string source("COPY THIS STRING WITH STL COPY");
std::string dest;
copy(source.begin(),source.end(),back_inserter(des t));
std::cout << dest << std::endl;
return 0;

this is slower because it doesn't take advantage of knowing the size of
the destination in advance.

An STL implementation of the for cycle is:

std::fill(src, src + recordLen, dest);

Best wishes,

Zeppe
Thx for the reminder!

--
Umut
Jun 27 '08 #5
On 2008-05-21 09:05:28 -0400, Zeppe
<ze***@remove.all.this.long.comment.yahoo.itsaid :
utab wrote:
>On Wed, 21 May 2008 05:46:29 -0700, fr33host wrote:
>>Dear developers,
I have a question. Is there a faster array copy than a for loop. What
would you suggest for this code sample?

void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}

Thank you for int main()

An option:

std::string source("COPY THIS STRING WITH STL COPY");
std::string dest;
copy(source.begin(),source.end(),back_inserter(des t));
std::cout << dest << std::endl;
return 0;

this is slower because it doesn't take advantage of knowing the size of
the destination in advance.

An STL implementation of the for cycle is:

std::fill(src, src + recordLen, dest);
fill takes a value, not an iterator, as its third argument.
std::unitialized_copy or std::copy would work, depending on whether the
destination is initialized (although that's not really an issue for
char values). memcpy also works fine, without having to decide whether
the target is initialized or whether that matters.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #6
Zeppe wrote:
>this is slower because it doesn't take advantage of knowing the size of
the destination in advance.

An STL implementation of the for cycle is:

std::fill(src, src + recordLen, dest);
I am also interested in an STL implementation of the mentioned for cycle
>>void get(char* src, char* dest, int i) {
for (int j = 0; j < recordLen; j++, i++) {
dest[j] = src[i];
}
}
and that takes advantage of knowing the size of the destination in
advance, but std::fill does not seem to do the trick for me. std::fill
stores the value dest at [src, src+recordLen). Or am I mistaken?

Matthias
Jun 27 '08 #7
Hi!

Zeppe schrieb:
utab wrote:
> std::string source("COPY THIS STRING WITH STL COPY");
std::string dest;
copy(source.begin(),source.end(),back_inserter(des t));
std::cout << dest << std::endl;
return 0;

this is slower because it doesn't take advantage of knowing the size of
the destination in advance.
Well:
std::string dest(source.begin(), source.end());

Or just:
std::string dest = source;

(which sometimes ends up to do no copying at all)

:)
Regards, Frank
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: stelladiary2004 | last post by:
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...
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...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
9
by: Peter Olcott | last post by:
// // Array2D.h 2005-11-27 5:50 AM // #define UINT unsigned int // // // class ArrayType2D { private: int Width;
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: ...
15
by: Michael A. Covington | last post by:
Any thoughts about how to implement image processing algorithms to run fast in C#? Using GetPixel to access every pixel from a Bitmap seems to be rather time-consuming.
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
2
by: barker7 | last post by:
I use a simple double array plus a variable to store the row size to represent two dimensional data. I need to quickly copy this data to a two dimensional array: double. Currently I iterate...
3
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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...

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.