| 
    bench-misc.c00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00045 
00046 #include <benchs.h>
00047 #include <stddef.h>
00048 #include <string.h>
00049 
00050 
00051 void bench_list (bench_t *b);
00052 void bench_strlen (bench_t *b);
00053 unsigned short fact_ushort (unsigned short n);
00054 unsigned long fact_ulong (unsigned long n);
00055 void bench_fact (bench_t *b);
00056 
00057 
00058 void
00059 bench_list (bench_t *b)
00060 {
00061   struct list {
00062     struct list *next;
00063   };
00064   struct list elts[100];
00065   struct list *first;
00066   struct list *n;
00067   int i;
00068 
00069   
00070   bench_start (b);
00071   first = 0;
00072   for (i = 0; i < 100; i++)
00073     {
00074       elts[i].next = first;
00075       first = &elts[i];
00076     }
00077   bench_stop (b);
00078   bench_report (b, "Single linked list init (100 elts)");
00079 
00080   
00081   i = 0;
00082   bench_start (b);
00083   for (n = first; n; n = n->next)
00084     i++;
00085   bench_stop (b);
00086   bench_report (b, "Scan list %d elts", (long) i);
00087 }
00088 
00089 const char *bench_string = "Hello World!";
00090 
00091 
00092 void
00093 bench_strlen (bench_t *b)
00094 {
00095   size_t l;
00096 
00097   
00098   bench_start (b);
00099   l = strlen ("Hello World!");
00100   bench_stop (b);
00101   bench_report (b, "strlen const string %d", (long) l);
00102 
00103   bench_start (b);
00104   l = strlen (bench_string);
00105   bench_stop (b);
00106   bench_report (b, "strlen %d", (long) l);  
00107 }
00108 
00109 unsigned short
00110 fact_ushort (unsigned short n)
00111 {
00112   if (n > 0)
00113     return n * fact_ushort (n - 1);
00114   else
00115     return 1;
00116 }
00117 
00118 unsigned long
00119 fact_ulong (unsigned long n)
00120 {
00121   if (n > 0)
00122     return n * fact_ulong (n - 1);
00123   else
00124     return 1;
00125 }
00126 
00127 void
00128 bench_fact (bench_t *b)
00129 {
00130   unsigned short f;
00131   unsigned long fl;
00132   
00133   bench_start (b);
00134   f = fact_ushort (8);
00135   bench_stop (b);
00136   bench_report (b, "fact(8) unsigned short (%d)", (long) f);
00137 
00138   bench_start (b);
00139   fl = fact_ulong (12);
00140   bench_stop (b);
00141   bench_report (b, "fact(12) unsigned long (%d)", fl);
00142 }
00143 
00144 
00145 int
00146 main ()
00147 {
00148   bench_t b;
00149 
00150   bench_init (&b);
00151   bench_list (&b);
00152   bench_strlen (&b);
00153   bench_fact (&b);
00154   return 0;
00155 }
 |