[Asterisk-Dev] FW: asterisk/doc CODING-GUIDELINES,1.4,1.5
Paul Cadach
paul at odt.east.telecom.kz
Wed Jan 19 05:49:42 MST 2005
Hello,
Andrew Kohlsmith wrote:
[skip]
> int count = 0;
> while (*(string++))
> count++;
>
> Yes my C's getting rusty but can you think of a better/faster way to count the
> characters in a zero-terminated string?
int strlen(register char *str)
{
register char *str1 = str;
while(*(str1++));
return(str1 - str - 1);
}
Just single increment within the loop (expression str1 - str - 1 have constant execution time, usually sub <reg>,<reg>
and dec <reg>, instead of count++ within loop body). Compiler's internal implementation of strlen() (when used with
key -O2 or higher) is always better than hand-written.
WBR,
Paul.
More information about the asterisk-dev
mailing list