fast array copy c++

F

fr33host

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;
}
}

Thank you for your answers.
 
U

utab

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;
}
}

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(dest));
std::cout << dest << std::endl;
return 0;
 
Z

Zeppe

utab said:
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;
}
}

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(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
 
U

utab

utab said:
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;
}
}

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(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!
 
M

Matthias Pfeifer

Zeppe said:
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;
}
}


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
 
F

Frank Birbacher

Hi!
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top