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. 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)
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
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
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
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)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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.
|
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?
|
by: Peter Olcott |
last post by:
//
// Array2D.h 2005-11-27 5:50 AM
//
#define UINT unsigned int
//
//
//
class ArrayType2D {
private:
int Width;
|
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__
| |
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.
|
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 | |
| ...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |