更快的字符串复制???? [英] FASTER string copy ????

查看:77
本文介绍了更快的字符串复制????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个结构定义如下:


typedef struct _sourceString

{

char firstSourceString [20];

char secondSourceString [20];

char thirdSourceString [20];

} sourceString;

typedef struct _destinString

{

char firstDestinString [20];

char secondDestinString [20];

char thirdDestinString [20];

} destinString;

代码中的
.......

/ ******** ************

sourceString * mySource;

destinString * myDestin;


mySource- > firstSourceString =" ABC 123";

mySource-> secondSourceString =" ABC 123";

mySource-> thirdSourceString =" ABC 123" ;;


strcpy(myDestin-> firstDestinString,mySource-> firstSourceString);

strcpy(myDestin-> secondDestinString,mySource-> secondSourceString);

strcpy (myDestin-> thirdDestinString,mySource-> thirdSourceString);

********************** /


上面的strcpy直到我把这个代码移植到一个小的嵌入式
系统,其中TIME是一个关键因素。这种类型的复制使用

" strcpy"需要更长的处理时间并产生一系列问题

与延迟相关。


但是,如何将源字符串复制到目标字符串

缓冲区而不使用strcpy(或memcpy)更快?

I have two structure definitions as below:

typedef struct _sourceString
{
char firstSourceString[20];
char secondSourceString[20];
char thirdSourceString[20];
}sourceString;
typedef struct _destinString
{
char firstDestinString[20];
char secondDestinString[20];
char thirdDestinString[20];
}destinString;

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

mySource->firstSourceString = "ABC 123";
mySource->secondSourceString = "ABC 123";
mySource->thirdSourceString = "ABC 123";

strcpy(myDestin->firstDestinString, mySource->firstSourceString);
strcpy(myDestin->secondDestinString, mySource->secondSourceString);
strcpy(myDestin->thirdDestinString, mySource->thirdSourceString);
**********************/

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 ?

推荐答案

ak ************** @ gmail.com 说:
但是,如何在不使用strcpy(或memcpy)的情况下将源字符串复制到目标字符串
缓冲区?
However, how can I copy my source strings to the destination string
buffers without using strcpy (or memcpy) faster ?




如果你可以击败平台优化的memcpy,你会做*非常好。

memcpy功能通常针对刀柄优化。


- -

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



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)


ak ************** @ gmail.com 写道:
我有两个结构定义如下:

typedef struct _sourceString
{char firstSourceString [20];
char secondSourceString [20];
char thirdSourceString [20];
} sourceString;

typedef struct _destinString
{char firstDestinString [20];
char secondDestinString [20];
char thirdDestinString [20];
} destinString;


看起来很傻,有两种不同的结构类型

,内容相同。不违法,只是傻。
代码中的
.......
/ ********************
sourceString * mySource;
destinString * myDestin;

mySource-> firstSourceString =" ABC 123" ;;


这是标准C中的错误:l.h.s.是一个数组,

和数组不可分配。如果你的编译器接受了

这个,它在某种伪C中运行。模式。

mySource-> secondSourceString =" ABC 123" ;;
mySource-> thirdSourceString =" ABC 123";

strcpy(myDestin- > firstDestinString,mySource-> firstSourceString);
strcpy(myDestin-> secondDestinString,mySource-> secondSourceString);
strcpy(myDestin-> thirdDestinString,mySource-> thirdSourceString); <
********************** /

上面的strcpy直到我将此代码移植到一个小型嵌入式系统,其中TIME是一个关键因素。这种类型的复制使用
strcpy需要更长的处理时间并产生与延迟相关的所有问题。

然而,如何在不使用strcpy(或memcpy)的情况下将源字符串复制到目标字符串
缓冲区)更快?
I have two structure definitions as below:

typedef struct _sourceString
{
char firstSourceString[20];
char secondSourceString[20];
char thirdSourceString[20];
}sourceString;
typedef struct _destinString
{
char firstDestinString[20];
char secondDestinString[20];
char thirdDestinString[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->firstSourceString = "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->secondSourceString = "ABC 123";
mySource->thirdSourceString = "ABC 123";

strcpy(myDestin->firstDestinString, mySource->firstSourceString);
strcpy(myDestin->secondDestinString, mySource->secondSourceString);
strcpy(myDestin->thirdDestinString, mySource->thirdSourceString);
**********************/

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 ?




我不知道。如果你的代码真的如你所示,

你使用的语言不是C而且我不知道

程序正在做什么。如果你的代码*不是*就像你已经显示的那样,那么,那么我还是*不能告诉你程序正在做什么!


...除此之外,C语言对各种构造的

速度一无所知,并且速度(绝对值和

相对值)将因实现而异。确定某些操作速度的唯一可靠方法是

测量需要多长时间 - 并记住测量值

仅在您创建它的系统上有效,使用编译器

版本和您使用的标志,以及在您使用它们的程序中使用它们。改变任何东西,你的尺寸变得可疑

如果不完全无效。


-

Eric Sosman
< a href =mailto:es ***** @ acm-dot-org.inva> es ***** @ acm-dot-org.inva lid



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


ak ************** @ gmail.com 写道:
ak**************@gmail.com wrote:
我有两个结构定义如下:

typedef struct _sourceString
{
char firstSourceString [20];
char secondSourceString [20];
char thirdSourceString [20];
} sourceString;

typedef struct _destinString
{
char firstDestinString [20];
char secondDestinString [20];
char thirdDestinString [20];
} destinString;

在代码中.... ..................................................................... /> mySource-> firstSourceString =" ABC 123" ;;


编译器错误:不是L值

未定义的行为:未初始化的指针被访问。

mySource-> secondSourceString =" ; ABC 123" ;;


同上

mySource-> thirdSourceString =" ABC 123" ;;


同上


发布原始(最少可编辑的)代码。

strcpy(myDestin-> firstDestinString, mySource-> firstSourceString);
strcpy(myDestin-> secondDestinString,mySource-> secondSourceString);
strcpy(myDestin-> thirdDestinString,mySource-> thirdSourceString);
* ********************* /

上面的strcpy直到我将此代码移植到一个小型嵌入式系统,其中TIME是一个关键因素。这种类型的复制使用
strcpy需要更长的处理时间并产生与延迟相关的所有问题。

然而,如何在不使用strcpy(或memcpy)的情况下将源字符串复制到目标字符串
缓冲区)更快?
I have two structure definitions as below:

typedef struct _sourceString
{
char firstSourceString[20];
char secondSourceString[20];
char thirdSourceString[20];
}sourceString;
typedef struct _destinString
{
char firstDestinString[20];
char secondDestinString[20];
char thirdDestinString[20];
}destinString;

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

mySource->firstSourceString = "ABC 123";
Compiler error : Not an L value
Undefined behavior : Un initialised pointer accessed.
mySource->secondSourceString = "ABC 123";
ditto
mySource->thirdSourceString = "ABC 123";
ditto

Post the original (minimal compilable) code.

strcpy(myDestin->firstDestinString, mySource->firstSourceString);
strcpy(myDestin->secondDestinString, mySource->secondSourceString);
strcpy(myDestin->thirdDestinString, mySource->thirdSourceString);
**********************/

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 ?



我不认为你可以编写一个比

编译器为这种情况提供的更快的函数。你只需分配结构就可以避免通过

调用strcpy。


* myDestin = * mySource


这可能效率不高,因为它会将一个

结构中的所有内容复制到另一个结构中。而strcpy只复制字符,直到

NUL。


如果你想要更高效的代码,可以考虑重新设计。尽量避免使用
不必要的副本等。


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.


这篇关于更快的字符串复制????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆