bench-string.c

Description | Download | Table of Contents | Modules | Compound List | File List | Functions


Overview
Compiler
Documentation
Examples
Misc
Help
IDE & Tools

Download
Install

Links
Projects






00001 /* Benchmark for some string functions
00002    Copyright (C) 2001 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@worldnet.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 
00055 /* The goal of this benchmark is to measure the generated code
00056    for some frequently used str* functions.  That's why we don't
00057    use any C-library here (such as newlib).  */
00058 
00059 char *
00060 strcpy (char * dest, const char *src)
00061 {
00062   char *tmp = dest;
00063 
00064   while ((*dest++ = *src++) != '\0')
00065     /* nothing */;
00066   return tmp;
00067 }
00068 
00069 char *
00070 strcat (char *dest, const char *src)
00071 {
00072   char *tmp = dest;
00073 
00074   while (*dest)
00075     dest++;
00076   while ((*dest++ = *src++) != '\0')
00077     ;
00078 
00079   return tmp;
00080 }
00081 
00082 int
00083 strcmp_for_bench (const char *cs, const char *ct)
00084 {
00085   register signed char __res;
00086 
00087   while (1) {
00088     if ((__res = *cs - *ct++) != 0 || !*cs++)
00089       break;
00090   }
00091 
00092   return __res;
00093 }
00094 
00095 char *
00096 strrchr (const char *s, int c)
00097 {
00098   const char *p = s + strlen(s);
00099   do {
00100     if (*p == (char)c)
00101       return (char *)p;
00102   } while (--p >= s);
00103   return NULL;
00104 }
00105 
00106 void *
00107 memcpy (void *dest, const void *src, size_t count)
00108 {
00109   char *tmp = (char *) dest, *s = (char *) src;
00110 
00111   while (count--)
00112     *tmp++ = *s++;
00113 
00114   return dest;
00115 }
00116 
00117 int
00118 memcmp (const void *cs, const void * ct, size_t count)
00119 {
00120   const unsigned char *su1, *su2;
00121   signed char res = 0;
00122 
00123   for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
00124     if ((res = *su1 - *su2) != 0)
00125       break;
00126   return res;
00127 }
00128 
00129 char *
00130 strstr (const char * s1, const char * s2)
00131 {
00132   int l1, l2;
00133 
00134   l2 = strlen(s2);
00135   if (!l2)
00136     return (char *) s1;
00137   l1 = strlen(s1);
00138   while (l1 >= l2) {
00139     l1--;
00140     if (!memcmp(s1,s2,l2))
00141       return (char *) s1;
00142     s1++;
00143   }
00144   return NULL;
00145 }
00146 #define MAX_LENGTH 64
00147 
00148 /* Benchmark the walk of a single linked list having 100 elements.  */
00149 void
00150 bench_string (bench_t *b)
00151 {
00152   char buf[MAX_LENGTH];
00153   int res;
00154   char *p;
00155   
00156   /* strcpy with a constant string.  */
00157   bench_start (b);
00158   strcpy (buf, "0");
00159   bench_stop (b);
00160   bench_report (b, "strcpy length %d", (long) strlen (buf));
00161 
00162   bench_start (b);
00163   strcpy (buf, "0123456789abcdef");
00164   bench_stop (b);
00165   bench_report (b, "strcpy length %d", (long) strlen (buf));
00166 
00167   bench_start (b);
00168   strcpy (buf, "0123456789abcdef0123456789abcdef");
00169   bench_stop (b);
00170   bench_report (b, "strcpy length %d", (long) strlen (buf));
00171 
00172   buf[0] = 0;
00173   bench_start (b);
00174   strcat (buf, "0");
00175   bench_stop (b);
00176   bench_report (b, "strcat length %d", (long) strlen (buf));
00177 
00178   bench_start (b);
00179   strcat (buf, "0123456789abcdef");
00180   bench_stop (b);
00181   bench_report (b, "strcat length %d", (long) strlen (buf));
00182   
00183   bench_start (b);
00184   strcat (buf, "0123456789abcdef");
00185   bench_stop (b);
00186   bench_report (b, "strcat length %d", (long) strlen (buf));
00187   
00188   strcpy (buf, "0");
00189   bench_start (b);
00190   res = strlen (buf);
00191   bench_stop (b);
00192   bench_report (b, "strlen length %d", (long) res);
00193 
00194   strcat (buf, "0123456789abcdef");
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   bench_start (b);
00207   res = strcmp_for_bench (buf, "0123456789abcdef0123456789abcdef");
00208   bench_stop (b);
00209   bench_report (b, "strcmp length %d, %d", (long) strlen (buf),
00210                 (long) res);
00211 
00212   bench_start (b);
00213   p = strrchr (buf, '0');
00214   bench_stop (b);
00215   bench_report (b, "strrchr at %d", (long) (size_t) (p - buf));
00216 
00217   bench_start (b);
00218   p = strstr (buf, "f0123456789abcdef");
00219   bench_stop (b);
00220   bench_report (b, "strstr at %d", (long) (size_t) (p - buf));
00221   
00222 }
00223 
00224 /* Main, run the benchmarks.  */
00225 int
00226 main ()
00227 {
00228   bench_t b;
00229 
00230   bench_init (&b);
00231   bench_string (&b);
00232   return 0;
00233 }
00234 

Description | Download | Table of Contents | Modules | Compound List | File List | Functions

    Last modified,
    Apr 16, 2001
[ Copying ]     [ Feedback to Stephane.Carrez@worldnet.fr ]