delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/01/11/07:59:06

To: gcc AT gcc DOT gnu DOT org
Cc: djgpp-workers AT delorie DOT com
Message-Id: <2.7.9.181SX.H8JVM1@pauzner.dnttm.ru>
From: "Leonid Pauzner" <uue AT pauzner DOT dnttm DOT ru>
Date: Sat, 11 Jan 2003 15:49:13 +0300 (MSK)
X-Mailer: dMail [Demos Mail for DOS v2.7.9]
Subject: gcc 3.2.1 optimizer degradation (strlen, -O2)
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com

Hi!

It turns out that the libc strlen() function (compiled with -O2)
became nearly 2 times slower when I switched from gcc 2.95.3 to gcc 3.2.1
on a Pentium machine.

Hand-optimised version improve performance of 3.2.1 compiled strlen code
up to the level of 2.95.3 (and with 2.95.3 both optimized and original
versions show the same performance).


This is DJGPP libc, strlen.c:
=============================
/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
#include <string.h>

size_t
strlen(const char *str)
{
  const char *s;

  if (str == 0)
    return 0;
  for (s = str; *s; ++s);
  return s-str;
}
=============================

My version (strlen2.c)
======================
#include <string.h>

size_t
strlen(const char *str)
{
  register const char *s = str;

  if (s == 0)
    return 0;
  while (*s++);
  return s-1-str;
}
======================

Sample test case:
=================
#include <string.h>

int main ()
{
  const char* str =
    "jhrfhruiehfiuerhifuhreihfgihrighihrfghkrjehklhlqkmjewbfjkfgtyhjjhgrwae3k";
  int len = strlen(str);
  int l, res;
  int n = 100000;

  for (; n > 0; n--) {
    l = len;
    while (l != 0) {
      res = strlen(str+l);  /* res = len - l */
      l = len - res;        /* touch res */
      l--;
    }
  }
  return 0;
}


- Raw text -


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