Re: Problem in Strdup()
- From: Mac <foo@xxxxxxx>
- Date: Wed, 31 Aug 2005 02:59:29 GMT
On Tue, 30 Aug 2005 04:59:07 +0000, Walter Roberson wrote:
> In article <pan.2005.08.30.04.12.42.949291@xxxxxxx>, Mac <foo@xxxxxxx> wrote:
>>On Mon, 29 Aug 2005 19:52:17 +0000, Keith Thompson wrote:
>
>>> strcpy() scans until it sees a '\0'. memcpy() scans for a specified
>>> number of bytes. It's not at all obvious that one is going to be
>>> faster than the other (though one or the other could well be on some
>>> particular platform).
>
>>Well, it is VERY hard for me to imagine that strcpy() will execute faster
>>than memcpy() when the string length is known ahead of time. I think in
>>general, memcpy() will be as fast as the implementers know how to make it.
>
> A memcpy() implementation could potentially detect the case where
> both memory areas had the same word alignment, copy a few bytes to
> reach the aligning point, and then copy words (or longwords) until
> it reached near the end and then copied bytes again. This would,
> for example, work if both areas happened to be the beginning of
> malloc()'d memory, as malloc() uses the most restrictive alignment.
> It could do similarily for the case where the areas could be aligned
> with shorter words... even moving by short int might be faster than
> moving by byte.
>
> Unfortunately, in a lot of cases, the two areas will not share any
> mutual alignment, such as if one is copying starting at an "odd" memory
> location and the source starts at an "even" location. And in that
> particular case, strcpy() is potentially faster. When the byte
> is moved, many architectures will automatically "test" the byte
> value, allowing a "branch on zero" without the overhead of an
> explicit test. That can be faster than decrementing a counter
> location (which requires a write operation, even if only a
> register write) and branching on the implicitly-tested value of it --
> it takes one less operation, since one has moved the byte anyhow...
I'm not going to enter into this kind of argument here. Anyone who cares
which way is faster should profile both ways. Enough said.
;-)
--Mac
.
- References:
- Problem in Strdup()
- From: priya
- Re: Problem in Strdup()
- From: Anton Petrusevich
- Re: Problem in Strdup()
- From: Walter Roberson
- Problem in Strdup()
- Prev by Date: Re: confused abt file operations
- Next by Date: Code consolidation
- Previous by thread: Re: Problem in Strdup()
- Next by thread: Re: Problem in Strdup()
- Index(es):
Relevant Pages
|