This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Two similar functions optimized differently


Hello,

  While playing with variations on a simple string comparing function,
I discovered that gcc will inline tolower differently for what appear
to be two almost identical functions.

  I'm using gcc-3.4.0 with -O2 for flags on an AMD Duron GNU/Linux
system.

The two functions, from slower to faster, are:


int string_compare (const char *s1, const char *s2) {
  while (*s1 && tolower ((unsigned char)*s1) == tolower ((unsigned char)*s2))
    s1++, s2++;

  return (tolower ((unsigned char)*s1) - tolower ((unsigned char)*s2));
}


int string_compare (const char *s1, const char *s2) {
  int i1, i2;


  i1 = tolower ((unsigned char)*s1++),
  i2 = tolower ((unsigned char)*s2++);
  while (i1 && i1 == i2) {
    i1 = tolower ((unsigned char)*s1++);
    i2 = tolower ((unsigned char)*s2++);
  }

  return (i1 - i2);
}


  In the second (faster) function, gcc will compile the code to call
__ctype_tolower_loc only once, which it then stores for later use. In
the first (slower) function, gcc will call __ctype_tolower_loc each
time through the loop.

  Changing the type of i1 and i2 to "char" does not affect this
behavior.

  If anyone would like the assembly output of both functions, or any
other information, feel free to ask. I'm rather naive about compilers,
so I can't begin to guess why there is such a difference between the
two functions, but my intuition tells me that gcc could do a better
job on the first if it's smart enough to inline so well on the second.


-- 
"There isn't enough darkness in the world to douse the light of a single
 candle."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]