473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FASTER string copy ????

I have two structure definitions as below:

typedef struct _sourceString
{
char firstSourceStri ng[20];
char secondSourceStr ing[20];
char thirdSourceStri ng[20];
}sourceString;
typedef struct _destinString
{
char firstDestinStri ng[20];
char secondDestinStr ing[20];
char thirdDestinStri ng[20];
}destinString;

in the code.......
/*************** *****
sourceString* mySource;
destinString* myDestin;

mySource->firstSourceStr ing = "ABC 123";
mySource->secondSourceSt ring = "ABC 123";
mySource->thirdSourceStr ing = "ABC 123";

strcpy(myDestin->firstDestinStr ing, mySource->firstSourceStr ing);
strcpy(myDestin->secondDestinSt ring, mySource->secondSourceSt ring);
strcpy(myDestin->thirdDestinStr ing, mySource->thirdSourceStr ing);
*************** *******/

The above "strcpy" is ok until I ported this code to a small embedded
system, where TIME is a critical factor. This type of copying by using
"strcpy" takes longer process time and creates all bunch of problems
related to delay.

However, how can I copy my source strings to the destination string
buffers without using strcpy (or memcpy) faster ?

Mar 17 '06 #1
11 4142
ak************* *@gmail.com said:
However, how can I copy my source strings to the destination string
buffers without using strcpy (or memcpy) faster ?


If you can beat a platform-optimised memcpy, you'll be doing *really* well.
The memcpy function is generally optimised to the hilt.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 17 '06 #2
ak************* *@gmail.com wrote:
I have two structure definitions as below:

typedef struct _sourceString
{
char firstSourceStri ng[20];
char secondSourceStr ing[20];
char thirdSourceStri ng[20];
}sourceString;
typedef struct _destinString
{
char firstDestinStri ng[20];
char secondDestinStr ing[20];
char thirdDestinStri ng[20];
}destinString;
Seems rather silly to have two different struct types
with identical content. Not illegal, just silly.
in the code.......
/*************** *****
sourceString* mySource;
destinString* myDestin;

mySource->firstSourceStr ing = "ABC 123";
This is an error in Standard C: the l.h.s. is an array,
and arrays are not assignable. If your compiler accepted
this, it is operating in some kind of "pseudo-C" mode.
mySource->secondSourceSt ring = "ABC 123";
mySource->thirdSourceStr ing = "ABC 123";

strcpy(myDestin->firstDestinStr ing, mySource->firstSourceStr ing);
strcpy(myDestin->secondDestinSt ring, mySource->secondSourceSt ring);
strcpy(myDestin->thirdDestinStr ing, mySource->thirdSourceStr ing);
*************** *******/

The above "strcpy" is ok until I ported this code to a small embedded
system, where TIME is a critical factor. This type of copying by using
"strcpy" takes longer process time and creates all bunch of problems
related to delay.

However, how can I copy my source strings to the destination string
buffers without using strcpy (or memcpy) faster ?


I have no idea. If your code is really as you have shown,
the language you are using isn't C and I cannot tell what the
program is doing. And if your code *isn't* as you have shown,
well then, I *still* cannot tell what the program is doing!

... besides which, the C language says nothing about the
speeds of various constructs, and the speeds (both absolute and
relative) will vary from one implementation to another. The only
reliable way to determine the speed of some operation is to
measure how long it takes -- and keep in mind that the measurement
is only valid on the system where you made it, with the compiler
version and flags that you used, and in the program where you
used them. Change anything, and your measurements become suspect
if not completely invalid.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 17 '06 #3
ak************* *@gmail.com wrote:
I have two structure definitions as below:

typedef struct _sourceString
{
char firstSourceStri ng[20];
char secondSourceStr ing[20];
char thirdSourceStri ng[20];
}sourceString;
typedef struct _destinString
{
char firstDestinStri ng[20];
char secondDestinStr ing[20];
char thirdDestinStri ng[20];
}destinString;

in the code.......
/*************** *****
sourceString* mySource;
destinString* myDestin;

mySource->firstSourceStr ing = "ABC 123";
Compiler error : Not an L value
Undefined behavior : Un initialised pointer accessed.
mySource->secondSourceSt ring = "ABC 123";
ditto
mySource->thirdSourceStr ing = "ABC 123";
ditto

Post the original (minimal compilable) code.

strcpy(myDestin->firstDestinStr ing, mySource->firstSourceStr ing);
strcpy(myDestin->secondDestinSt ring, mySource->secondSourceSt ring);
strcpy(myDestin->thirdDestinStr ing, mySource->thirdSourceStr ing);
*************** *******/

The above "strcpy" is ok until I ported this code to a small embedded
system, where TIME is a critical factor. This type of copying by using
"strcpy" takes longer process time and creates all bunch of problems
related to delay.

However, how can I copy my source strings to the destination string
buffers without using strcpy (or memcpy) faster ?

I dont think you can write a function that is faster than what the
compiler provides for this case. You can avoid the call to strcpy by
just assigning the structures.

*myDestin = *mySource

This may not be efficient because it will copy all the contents in one
structure to the other. Whereas strcpy copies only the characters up to
NUL.

If you want more efficient code consider redesigning. Try avoiding
unnecessary copies and the like.

Mar 17 '06 #4
On Thu, 16 Mar 2006 20:34:05 -0800, akarui.tomodach i wrote:

The above "strcpy" is ok until I ported this code to a small embedded
system, where TIME is a critical factor. This type of copying by using
"strcpy" takes longer process time and creates all bunch of problems
related to delay.

However, how can I copy my source strings to the destination string
buffers without using strcpy (or memcpy) faster ?


memcpy() will likely be the fastest way you could copy anything to
anywhere for general purpose opaque data.

What's even faster than copying with memcpy() is not copying at all,
so called "zero-copy". With that methodology you simply pass pointers to
your buffers around. It's rather difficult to do, though, especially if
your design didn't incorporate the notion from the beginning. When you
start contemplating reference counting or some such device it's probably
too late in the design cycle (unless the purpose of the device is
specific, and the scope of use limited).

The simper way is for ownership of the buffer to implicitly pass to the
various functions, but that requires careful attention to the flow of
logic.
Mar 17 '06 #5
On 2006-03-17, Richard Heathfield <in*****@invali d.invalid> wrote:
If you can beat a platform-optimised memcpy, you'll be doing *really* well.
The memcpy function is generally optimised to the hilt.


Not always true. For example, I've done some programming on the nintendo
gameboy advance, and the memcpy() provided by newlib (an
embedded-oriented libc) when compiled with target=arm-agb-elf does *not*
use the DMA circuitry of the gba. So if you use that, you get much
faster memory copies.

But that's a pathological case, I do agree that in general memcpy tends
to be very optimized, and the best choice.

--
John Tsiombikas (Nuclear / Mindlapse)
nu*****@siggrap h.org
http://nuclear.demoscene.gr/
Mar 17 '06 #6
John Tsiombikas (Nuclear / Mindlapse) said:
On 2006-03-17, Richard Heathfield <in*****@invali d.invalid> wrote:
If you can beat a platform-optimised memcpy, you'll be doing *really*
well. The memcpy function is generally optimised to the hilt.


Not always true. For example, I've done some programming on the nintendo
gameboy advance, and the memcpy() provided by newlib (an
embedded-oriented libc) when compiled with target=arm-agb-elf does *not*
use the DMA circuitry of the gba. So if you use that, you get much
faster memory copies.


If that is the case, then it isn't platform-optimised. Any fool can write a
/slow/ memcpy:

volatile time_t _lets_waste_tim e;void*memcpy(v oid*t,const void*s,size_t n)
{void*u=t;size_ t i;while(n--){for(i=0;i<n;i ++)_lets_waste_ time=time(NULL) ;
((unsigned char*)t)[n]=((unsigned char*)s)[n];}return u;}

or some such nonsense. This doesn't affect my argument in the slightest.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 17 '06 #7
On 2006-03-17, Richard Heathfield <in*****@invali d.invalid> wrote:
John Tsiombikas (Nuclear / Mindlapse) said:
On 2006-03-17, Richard Heathfield <in*****@invali d.invalid> wrote:
If you can beat a platform-optimised memcpy, you'll be doing *really*
well. The memcpy function is generally optimised to the hilt.


Not always true. For example, I've done some programming on the nintendo
gameboy advance, and the memcpy() provided by newlib (an
embedded-oriented libc) when compiled with target=arm-agb-elf does *not*
use the DMA circuitry of the gba. So if you use that, you get much
faster memory copies.


If that is the case, then it isn't platform-optimised. Any fool can write a
/slow/ memcpy:

volatile time_t _lets_waste_tim e;void*memcpy(v oid*t,const void*s,size_t n)
{void*u=t;size_ t i;while(n--){for(i=0;i<n;i ++)_lets_waste_ time=time(NULL) ;
((unsigned char*)t)[n]=((unsigned char*)s)[n];}return u;}

or some such nonsense. This doesn't affect my argument in the slightest.


I didn't say it does. I just said that in general what you said is true,
and memcpy is indeed optimized "to the hilt" as you put it, but there
are cases where the memcpy provided might not be platform-optimized, and
it would be a good idea for one to check that first, if memory copy
speed is essential.

--
John Tsiombikas (Nuclear / Mindlapse)
nu*****@siggrap h.org
http://nuclear.demoscene.gr/
Mar 17 '06 #8
John Tsiombikas (Nuclear / Mindlapse) said:
On 2006-03-17, Richard Heathfield <in*****@invali d.invalid> wrote:

This doesn't affect my argument in the slightest.


I didn't say it does. I just said that in general what you said is true,
and memcpy is indeed optimized "to the hilt" as you put it, but there
are cases where the memcpy provided might not be platform-optimized, and
it would be a good idea for one to check that first, if memory copy
speed is essential.


Yes, fair enough.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 17 '06 #9
Hi Eric,
You are right, I didn't put the proper syntax in assignment to the
string buffer. The excat code is here:

typedef struct _sourceString
{
char firstSourceStri ng[20];
char secondSourceStr ing[20];
char thirdSourceStri ng[20];

}sourceString;

typedef struct _destinString
{
char firstDestinStri ng[20];
char secondDestinStr ing[20];
char thirdDestinStri ng[20];

}destinString;

in the code.......
/*************** *****
sourceString* mySource;
destinString* myDestin;

sprintf(mySourc e->firstSourceStr ing, "%s", "ABC 123");
sprintf(mySourc e->secondSourceSt ring,"%s", "ABC 123");
sprintf(mySourc e->thirdSourceStr ing, "%s", "ABC 123");

strcpy(myDestin->firstDestinStr ing, mySource->firstSourceStr ing);
strcpy(myDestin->secondDestinSt ring, mySource->secondSourceSt ring);
strcpy(myDestin->thirdDestinStr ing, mySource->thirdSourceStr ing);
*************** *******/

Mar 17 '06 #10

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

Similar topics

23
4717
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
98
14338
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
43
6853
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app...
9
5153
by: VenuGopal | last post by:
Hi, why n++ executes faster than n+1..... or does it realli execute faster? thanks Venugopal.B
13
2532
by: Niyazi | last post by:
Hi I have a report that I have to run it monthly in my machine. My code in VB.NET and I access AS400 to get data, anaysie it and send into pre formated Excel sheet. The data consist of 9000 rows. I use data table and with for loop I send the data row by row in pre-formated Excel sheet. My machine is:
9
2046
by: Russell Mangel | last post by:
Can someone show me how to speed this up? 1. Whats the fastest way for Unsafe C#? 2. What the fastest way for Safe C#? public static Int64 ToInt64(Int32 low, Int32 high) { Byte lowBytes = BitConverter.GetBytes(low); Byte highBytes = BitConverter.GetBytes(high);
34
3511
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical...
0
7703
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...
0
7618
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...
0
8138
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...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
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...

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.