pen_strdup (0.17.1) speed up

From: Nigel Horne <njh_at_bandsman.co.uk>
Date: Mon, 27 Aug 2007 08:56:49 +0100

The length of the string is calculated twice, once by strlen and once (by implication of
looking for the NUL terminator) by strcpy.

Better to calculated it once and remember:

*** Open.c 2007-08-27 08:53:40.000000000 +0100
--- pen.c 2007-08-27 08:55:50.000000000 +0100
***************
*** 241,248 ****
  
  static char *pen_strdup(char *p)
  {
! char *b = pen_malloc(strlen(p)+1);
! return strcpy(b, p);
  }
  
  static int pen_strncasecmp(const char *p, const char *q, size_t n)
--- 241,254 ----
  
  static char *pen_strdup(char *p)
  {
! size_t len = strlen(p);
! char *b = pen_malloc(len + 1);
!
! if(b) {
! memcpy(b, p, len);
! b[len] = '\0';
! }
! return b;
  }
  
  static int pen_strncasecmp(const char *p, const char *q, size_t n)

-- 
Nigel Horne. Adjudicator, Arranger, Band Trainer, Composer, Tutor, Typesetter.
NJH Music, Barnsley, UK.  ICQ#20252325
njh@bandsman.co.uk http://www.bandsman.co.uk

Received on Mon Aug 27 2007 - 10:33:57 CEST

This archive was generated by hypermail 2.2.0 : Mon Aug 27 2007 - 10:33:58 CEST