www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/21/04:25:22

Date: Tue, 21 Jul 1998 11:22:49 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Mariano Alvarez Fernández <malfer AT teleline DOT es>
cc: djgpp AT delorie DOT com
Subject: Re: About DJGPP v2.02, more results
In-Reply-To: <35B38619.4E02@teleline.es>
Message-ID: <Pine.SUN.3.91.980721112221.6992L-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019