bench-misc.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 misc 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 
00045 
00046 #include <benchs.h>
00047 #include <stddef.h>
00048 
00049 /* Benchmark the walk of a single linked list having 100 elements.  */
00050 void
00051 bench_list (bench_t *b)
00052 {
00053   struct list {
00054     struct list *next;
00055   };
00056   struct list elts[100];
00057   struct list *first;
00058   struct list *n;
00059   int i;
00060 
00061   /* Build the list.  */
00062   bench_start (b);
00063   first = 0;
00064   for (i = 0; i < 100; i++)
00065     {
00066       elts[i].next = first;
00067       first = &elts[i];
00068     }
00069   bench_stop (b);
00070   bench_report (b, "Single linked list init (100 elts)");
00071 
00072   /* Scan the list.  */
00073   i = 0;
00074   bench_start (b);
00075   for (n = first; n; n = n->next)
00076     i++;
00077   bench_stop (b);
00078   bench_report (b, "Scan list %d elts", (long) i);
00079 }
00080 
00081 const char *bench_string = "Hello World!";
00082 
00083 /* Benchmark of strlen.  */
00084 void
00085 bench_strlen (bench_t *b)
00086 {
00087   size_t l;
00088 
00089   /* Gcc 3.0 computes the lenght of the string.  Gcc 2.95 does not.  */
00090   bench_start (b);
00091   l = strlen ("Hello World!");
00092   bench_stop (b);
00093   bench_report (b, "strlen const string %d", (long) l);
00094 
00095   bench_start (b);
00096   l = strlen (bench_string);
00097   bench_stop (b);
00098   bench_report (b, "strlen %d", (long) l);  
00099 }
00100 
00101 unsigned short
00102 fact_ushort (unsigned short n)
00103 {
00104   if (n > 0)
00105     return n * fact_ushort (n - 1);
00106   else
00107     return 1;
00108 }
00109 
00110 unsigned long
00111 fact_ulong (unsigned long n)
00112 {
00113   if (n > 0)
00114     return n * fact_ulong (n - 1);
00115   else
00116     return 1;
00117 }
00118 
00119 void
00120 bench_fact (bench_t *b)
00121 {
00122   unsigned short f;
00123   unsigned long fl;
00124   
00125   bench_start (b);
00126   f = fact_ushort (8);
00127   bench_stop (b);
00128   bench_report (b, "fact(8) unsigned short (%d)", (long) f);
00129 
00130   bench_start (b);
00131   fl = fact_ulong (12);
00132   bench_stop (b);
00133   bench_report (b, "fact(12) unsigned long (%d)", fl);
00134 }
00135 
00136 /* Main, run the benchmarks.  */
00137 int
00138 main ()
00139 {
00140   bench_t b;
00141 
00142   bench_init (&b);
00143   bench_list (&b);
00144   bench_strlen (&b);
00145   bench_fact (&b);
00146   return 0;
00147 }

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

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