"Fossies" - the Fresh Open Source Software Archive

Member "bc-1.06.95/lib/testmul.c" (11 Jun 2006, 4619 Bytes) of package /linux/misc/old/bc-1.06.95.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "testmul.c" see the Fossies "Dox" file reference documentation.

    1 /* compute the crossover for recursive and simple multiplication */
    2 
    3 #include <stdio.h>
    4 #include <stdlib.h>
    5 #include <string.h>
    6 #include <time.h>
    7 #include "number.h"
    8 #ifndef VARARGS
    9 #include <stdarg.h>
   10 #else
   11 #include <varargs.h>
   12 #endif
   13 
   14 /* from number.c ... */
   15 extern int mul_base_digits;
   16 /* extern int mul_small_digits; */
   17 extern bc_num _one_;
   18 
   19 /* global variables */
   20 int test_n = 1000;
   21 long test_time = 30 * CLOCKS_PER_SEC;  /* 30 seconds */ 
   22 
   23 /* Other things for number.c. */
   24 int std_only;
   25 
   26 void
   27 out_of_memory()
   28 {
   29   fprintf (stderr, "Fatal error: Out of memory for malloc.\n");
   30   exit (1);
   31 }
   32 
   33 /* Runtime error will  print a message and stop the machine. */
   34 
   35 #ifndef VARARGS
   36 #ifdef __STDC__
   37 void
   38 rt_error (char *mesg, ...)
   39 #else
   40 void
   41 rt_error (mesg)
   42      char *mesg;
   43 #endif
   44 #else
   45 void
   46 rt_error (mesg, va_alist)
   47      char *mesg;
   48 #endif
   49 {
   50   va_list args;
   51   char error_mesg [255];
   52 
   53 #ifndef VARARGS   
   54   va_start (args, mesg);
   55 #else
   56   va_start (args);
   57 #endif
   58   vsprintf (error_mesg, mesg, args);
   59   va_end (args);
   60   
   61   fprintf (stderr, "Runtime error: %s\n", error_mesg);
   62 }
   63 
   64 /* A runtime warning tells of some action taken by the processor that
   65    may change the program execution but was not enough of a problem
   66    to stop the execution. */
   67 
   68 #ifndef VARARGS
   69 #ifdef __STDC__
   70 void
   71 rt_warn (char *mesg, ...)
   72 #else
   73 void
   74 rt_warn (mesg)
   75      char *mesg;
   76 #endif
   77 #else
   78 void
   79 rt_warn (mesg, va_alist)
   80      char *mesg;
   81 #endif
   82 {
   83   va_list args;
   84   char error_mesg [255];
   85 
   86 #ifndef VARARGS   
   87   va_start (args, mesg);
   88 #else
   89   va_start (args);
   90 #endif
   91   vsprintf (error_mesg, mesg, args);
   92   va_end (args);
   93 
   94   fprintf (stderr, "Runtime warning: %s\n", error_mesg);
   95 }
   96 
   97 void
   98 out_char (int ch)
   99 {
  100   putchar (ch);
  101 }
  102 
  103 /* Time stuff !!! */
  104 
  105 int
  106 timeit ( bc_num a, bc_num b, int *n)
  107 {
  108   clock_t first;
  109   int i, res;
  110   bc_num c;
  111 
  112   bc_init_num (&c);
  113   first = clock();
  114   *n = 0;
  115   do {
  116     for (i=0; i<test_n; i++)
  117       bc_multiply(a,b,&c,0);
  118     *n += test_n;
  119      res = (int) (clock() - first);
  120   } while (res < test_time);
  121   return res;
  122 }
  123 
  124 int debug = 0;  /* Print debugging messages? */
  125 
  126 int main (int argc, char **argv)
  127 {
  128   bc_num ten, num, expo, big;
  129 
  130   int min, max, mid;
  131 
  132 #if 0
  133   int smallsize;
  134 #endif
  135 
  136   int n1, n2;
  137   clock_t t1, t2;
  138   float permul1, permul2;
  139 
  140   /* args? */
  141   if (argc > 1)
  142     if (strcmp (argv[1], "-d") == 0)
  143       debug = 1;
  144 
  145   bc_init_numbers();
  146   bc_init_num (&ten);
  147   bc_init_num (&num);
  148   bc_init_num (&expo);
  149   bc_init_num (&big);
  150   bc_int2num (&ten, 10);
  151 
  152   if (debug)
  153     fprintf (stderr, "Timings are for %d multiplies\n"
  154                  "Minimum time is %lu seconds\n", test_n,
  155          test_time/CLOCKS_PER_SEC);
  156 
  157   /* Two of the same size */
  158   min = 10;
  159   max = 500;
  160 
  161   if (debug)
  162     fprintf (stderr, "Testing numbers of the same length.\n");
  163 
  164   while (min < max) {
  165     mid = (min+max)/2;
  166     if (debug) fprintf (stderr,"Checking %d...\n", mid);
  167 
  168     bc_int2num (&expo, mid);
  169     bc_raise (ten, expo, &num, 0);
  170     bc_sub (num, _one_, &num, 0);
  171 
  172     mul_base_digits = 2*mid+1;
  173     t1 = timeit (num, num, &n1);
  174     permul1 = (float)t1/(float)n1;
  175 
  176     mul_base_digits = 2*mid-1;
  177     t2 = timeit (num, num, &n2);
  178     permul2 = (float)t2/(float)n2;
  179 
  180     if (permul1 < permul2)
  181       min = mid+1;
  182     else
  183       max = mid-1;
  184 
  185     if (debug) {
  186       fprintf (stderr, "n1 = %d :: n2 = %d\n", n1, n2);
  187       fprintf (stderr, "p1 = %f :: p2 = %f\n", permul1, permul2);
  188     }
  189   }  
  190 
  191   if (debug)
  192     fprintf (stderr, "Base digits crossover at %d digits\n", min);
  193   printf ("#define MUL_BASE_DIGITS %d\n", 2*min);
  194 
  195 
  196 #if 0
  197   mul_base_digits = min;
  198 
  199   /* Small one times a big one. */
  200 
  201   smallsize = min/2;
  202   bc_int2num (&expo, smallsize);
  203   bc_raise (ten, expo, &big, 0);
  204   bc_sub (num, _one_, &big, 0);
  205 
  206   min = min / 2;
  207   max = 500;
  208 
  209   if (debug)
  210     fprintf (stderr, "Testing numbers of the different length.\n");
  211 
  212   while (min < max) {
  213     mid = (min+max)/2;
  214     if (debug) fprintf (stderr, "Checking %d...\n", mid);
  215 
  216     bc_int2num (&expo, mid-smallsize);
  217     bc_raise (ten, expo, &num, 0);
  218     bc_sub (num, _one_, &num, 0);
  219 
  220     mul_small_digits = mid+1;
  221     t1 = timeit (big, num, &n1);
  222     permul1 = (float)t1/(float)n1;
  223 
  224     mul_small_digits = mid-1;
  225     t2 = timeit (big, num, &n2);
  226     permul2 = (float)t2/(float)n2;
  227 
  228     if (permul1 < permul2)
  229       min = mid+1;
  230     else
  231       max = mid-1;
  232 
  233     if (debug) {
  234       fprintf (stderr, "n1 = %d :: n2 = %d\n", n1, n2);
  235       fprintf (stderr, "p1 = %f :: p2 = %f\n", permul1, permul2);
  236     }
  237   }  
  238   
  239   if (debug)
  240     fprintf (stderr, "Non equal digits crossover at %d total digits\n", min);
  241   printf ("#define MUL_SMALL_DIGITS = %d\n", min);
  242 
  243 #endif
  244 
  245   return 0;
  246 }