470,815 Members | 1,098 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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 3426
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by stelladiary2004 | last post: by
7 posts views Thread by simkn | last post: by
10 posts views Thread by javuchi | last post: by
15 posts views Thread by Michael A. Covington | last post: by
95 posts views Thread by hstagni | last post: by
2 posts views Thread by barker7 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.