bench-string.c

00001 /* Benchmark for some string functions
00002    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00051 
00052 #include <benchs.h>
00053 #include <stddef.h>
00054 #include <string.h>
00055 
00056 /* Forward declaration.  */
00057 int strcmp_for_bench (const char *s1, const char *s2);
00058 void bench_string (bench_t *b);
00059 
00060 
00061 /* The goal of this benchmark is to measure the generated code
00062    for some frequently used str* functions.  That's why we don't
00063    use any C-library here (such as newlib).  */
00064 
00065 char *
00066 strcpy (char * dest, const char *src)
00067 {
00068   char *tmp = dest;
00069 
00070   while ((*dest++ = *src++) != '\0')
00071     /* nothing */;
00072   return tmp;
00073 }
00074 
00075 char *
00076 strcat (char *dest, const char *src)
00077 {
00078   char *tmp = dest;
00079 
00080   while (*dest)
00081     dest++;
00082   while ((*dest++ = *src++) != '\0')
00083     ;
00084 
00085   return tmp;
00086 }
00087 
00088 int
00089 strcmp_for_bench (const char *cs, const char *ct)
00090 {
00091   register signed char __res;
00092 
00093   while (1) {
00094     if ((__res = *cs - *ct++) != 0 || !*cs++)
00095       break;
00096   }
00097 
00098   return __res;
00099 }
00100 
00101 char *
00102 strrchr (const char *s, int c)
00103 {
00104   const char *p = s + strlen(s);
00105   do {
00106     if (*p == (char)c)
00107       return (char *)p;
00108   } while (--p >= s);
00109   return NULL;
00110 }
00111 
00112 void *
00113 memcpy (void *dest, const void *src, size_t count)
00114 {
00115   char *tmp = (char *) dest, *s = (char *) src;
00116 
00117   while (count--)
00118     *tmp++ = *s++;
00119 
00120   return dest;
00121 }
00122 
00123 int
00124 memcmp (const void *cs, const void * ct, size_t count)
00125 {
00126   const unsigned char *su1, *su2;
00127   signed char res = 0;
00128 
00129   for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
00130     if ((res = *su1 - *su2) != 0)
00131       break;
00132   return res;
00133 }
00134 
00135 char *
00136 strstr (const char * s1, const char * s2)
00137 {
00138   int l1, l2;
00139 
00140   l2 = strlen(s2);
00141   if (!l2)
00142     return (char *) s1;
00143   l1 = strlen(s1);
00144   while (l1 >= l2) {
00145     l1--;
00146     if (!memcmp(s1,s2,l2))
00147       return (char *) s1;
00148     s1++;
00149   }
00150   return NULL;
00151 }
00152 #define MAX_LENGTH 64
00153 
00154 /* Benchmark the walk of a single linked list having 100 elements.  */
00155 void
00156 bench_string (bench_t *b)
00157 {
00158   char buf[MAX_LENGTH];
00159   int res;
00160   char *p;
00161   
00162   /* strcpy with a constant string.  */
00163   bench_start (b);
00164   strcpy (buf, "0");
00165   bench_stop (b);
00166   bench_report (b, "strcpy length %d", (long) strlen (buf));
00167 
00168   bench_start (b);
00169   strcpy (buf, "0123456789abcdef");
00170   bench_stop (b);
00171   bench_report (b, "strcpy length %d", (long) strlen (buf));
00172 
00173   bench_start (b);
00174   strcpy (buf, "0123456789abcdef0123456789abcdef");
00175   bench_stop (b);
00176   bench_report (b, "strcpy length %d", (long) strlen (buf));
00177 
00178   buf[0] = 0;
00179   bench_start (b);
00180   strcat (buf, "0");
00181   bench_stop (b);
00182   bench_report (b, "strcat length %d", (long) strlen (buf));
00183 
00184   bench_start (b);
00185   strcat (buf, "0123456789abcdef");
00186   bench_stop (b);
00187   bench_report (b, "strcat length %d", (long) strlen (buf));
00188   
00189   bench_start (b);
00190   strcat (buf, "0123456789abcdef");
00191   bench_stop (b);
00192   bench_report (b, "strcat length %d", (long) strlen (buf));
00193   
00194   strcpy (buf, "0");
00195   bench_start (b);
00196   res = strlen (buf);
00197   bench_stop (b);
00198   bench_report (b, "strlen length %d", (long) res);
00199 
00200   strcat (buf, "0123456789abcdef");
00201   bench_start (b);
00202   res = strlen (buf);
00203   bench_stop (b);
00204   bench_report (b, "strlen length %d", (long) res);
00205 
00206   strcat (buf, "0123456789abcdef");
00207   bench_start (b);
00208   res = strlen (buf);
00209   bench_stop (b);
00210   bench_report (b, "strlen length %d", (long) res);
00211 
00212   bench_start (b);
00213   res = strcmp_for_bench (buf, "0123456789abcdef0123456789abcdef");
00214   bench_stop (b);
00215   bench_report (b, "strcmp length %d, %d", (long) strlen (buf),
00216                 (long) res);
00217 
00218   bench_start (b);
00219   p = strrchr (buf, '0');
00220   bench_stop (b);
00221   bench_report (b, "strrchr at %d", (long) (size_t) (p - buf));
00222 
00223   bench_start (b);
00224   p = strstr (buf, "f0123456789abcdef");
00225   bench_stop (b);
00226   bench_report (b, "strstr at %d", (long) (size_t) (p - buf));
00227   
00228 }
00229 
00230 /* Main, run the benchmarks.  */
00231 int
00232 main ()
00233 {
00234   bench_t b;
00235 
00236   bench_init (&b);
00237   bench_string (&b);
00238   return 0;
00239 }
00240