473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3631
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.beg in(),source.end (),back_inserte r(dest));
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.beg in(),source.end (),back_inserte r(dest));
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.beg in(),source.end (),back_inserte r(dest));
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.a ll.this.long.co mment.yahoo.its aid:
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.beg in(),source.end (),back_inserte r(dest));
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::unitialize d_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.beg in(),source.end (),back_inserte r(dest));
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.beg in(), 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
12064
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 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:
7
7417
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.
20
9180
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., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
9
2073
by: Peter Olcott | last post by:
// // Array2D.h 2005-11-27 5:50 AM // #define UINT unsigned int // // // class ArrayType2D { private: int Width;
10
3016
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__
15
18762
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
5430
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 ____________________________________ | | | ------------------ | | | BUTTON | | | ...
2
4017
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 through the double, one by one and place it into the preallocated double. The arrays are large, and I'm doing a lot of these. Thus it has become a significant bottleneck. Can anyone suggest a faster way to convert a double + row size info to a...
3
6227
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 has to tell then them its wrong information but currently it takes then to a next page and then tells them its incorrect information. This is tedious as every time they enter wrong they will be redirected to a different page and then they have to...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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
10237
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
10029
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...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.