00001 /* Benchmark for some misc 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 00045 00046 #include <benchs.h> 00047 #include <stddef.h> 00048 #include <string.h> 00049 00050 /* Forward declaration. */ 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 /* Benchmark the walk of a single linked list having 100 elements. */ 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 /* Build the list. */ 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 /* Scan the list. */ 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 /* Benchmark of strlen. */ 00092 void 00093 bench_strlen (bench_t *b) 00094 { 00095 size_t l; 00096 00097 /* Gcc 3.0 computes the lenght of the string. Gcc 2.95 does not. */ 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 /* Main, run the benchmarks. */ 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 }