Mail Archives: djgpp/1998/07/21/04:25:22
On Mon, 20 Jul 1998, Mariano Alvarez Fernández wrote:
> > These entries seem to imply that you call strlen for each call to
> > strcpy/strncpy. It would be faster to call memcpy/memmove instead,
> > since once you know the length of a string, there's no reason to use
> > the string-copy functions (which are slower because they look for the
> > terminating null character).
>
> It's the dbf format too. In dbf char strings are blank filled, I use
> null terminated strings internally, strcopnb do the input conversion.
>
> int strcopnb( char *dest, char *org, int max )
> {
> int i;
>
> strncpy( dest,org,max );
> dest[max] = '\0';
> max = strlen( dest );
> for( i=max-1; i >= 0 && dest[i] == ' '; i-- );
> dest[i+1] = '\0';
> return 0;
> }
I think this should be faster:
int strcopnb( char *dest, char *org, int max )
{
int i;
memcpy( dest,org,max );
dest[max] = '\0';
/* max = strlen( dest ); redundant: we just put '\0' at dest[max] */
for( i=max-1; i >= 0 && dest[i] == ' '; i-- );
dest[i+1] = '\0';
return 0;
}
This thread is quickly degenerating into a C optimization class ;-).
- Raw text -