Re: faster strcpy()

Tim Smith (tzs@halcyon.com)
Sat, 25 Apr 1998 03:17:58 -0700


At 08:22 AM 4/24/98 -0400, Richard B. Johnson wrote:
>> #define strcpy(a,b) (char *)memcpy(a,b,strlen(b))
...
>Could you please describe how you measured its speed? You actually
>more than doubled the number of CPU instructions that need to be
>executed to perform this function.

You aren't counting right. The standard C while loop version does
~N reads and N writes, where N is the length of the string, plus N
tests and N jumps.

A strlen will do ~N reads, N tests, and N jumps. Even a slightly
optimized memcpy (no tricks with floating point registers or anything
like that, just longword moving) will do ~N/4 reads, N/4 writes, N/4
tests, and N/4 jumps. Total reads, tests, and jumps are up by 25%,
but writes are down by 75%. It is nowhere near double the number
of instructions. If the memcpy unrolls the loop, the strlen + memcpy
version is likely to use less instructions.

--Tim Smith

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu