Back to index

lightning-sunbird  0.9+nobinonly
mptest-5a.c
Go to the documentation of this file.
00001 /*
00002  * Simple test driver for MPI library
00003  *
00004  * Test 5a: Greatest common divisor speed test, binary vs. Euclid
00005  *
00006  * ***** BEGIN LICENSE BLOCK *****
00007  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00008  *
00009  * The contents of this file are subject to the Mozilla Public License Version
00010  * 1.1 (the "License"); you may not use this file except in compliance with
00011  * the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Software distributed under the License is distributed on an "AS IS" basis,
00015  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  * for the specific language governing rights and limitations under the
00017  * License.
00018  *
00019  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00020  *
00021  * The Initial Developer of the Original Code is
00022  * Michael J. Fromberger.
00023  * Portions created by the Initial Developer are Copyright (C) 1998
00024  * the Initial Developer. All Rights Reserved.
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 /* $Id: mptest-5a.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <ctype.h>
00047 #include <limits.h>
00048 #include <time.h>
00049 
00050 #include <sys/time.h>
00051 
00052 #include "mpi.h"
00053 #include "mpprime.h"
00054 
00055 typedef struct {
00056   unsigned int  sec;
00057   unsigned int  usec;
00058 } instant_t;
00059 
00060 instant_t now(void)
00061 {
00062   struct timeval clk;
00063   instant_t      res;
00064 
00065   res.sec = res.usec = 0;
00066 
00067   if(gettimeofday(&clk, NULL) != 0)
00068     return res;
00069 
00070   res.sec = clk.tv_sec;
00071   res.usec = clk.tv_usec;
00072 
00073   return res;
00074 }
00075 
00076 #define PRECISION 16
00077 
00078 int main(int argc, char *argv[])
00079 {
00080   int          ix, num, prec = PRECISION;
00081   mp_int       a, b, c, d;
00082   instant_t    start, finish;
00083   time_t       seed;
00084   unsigned int d1, d2;
00085 
00086   seed = time(NULL);
00087 
00088   if(argc < 2) {
00089     fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
00090     return 1;
00091   }
00092 
00093   if((num = atoi(argv[1])) < 0)
00094     num = -num;
00095 
00096   printf("Test 5a: Euclid vs. Binary, a GCD speed test\n\n"
00097         "Number of tests: %d\n"
00098         "Precision:       %d digits\n\n", num, prec);
00099 
00100   mp_init_size(&a, prec);
00101   mp_init_size(&b, prec);
00102   mp_init(&c);
00103   mp_init(&d);
00104 
00105   printf("Verifying accuracy ... \n");
00106   srand((unsigned int)seed);
00107   for(ix = 0; ix < num; ix++) {
00108     mpp_random_size(&a, prec);
00109     mpp_random_size(&b, prec);
00110 
00111     mp_gcd(&a, &b, &c);
00112     mp_bgcd(&a, &b, &d);
00113 
00114     if(mp_cmp(&c, &d) != 0) {
00115       printf("Error!  Results not accurate:\n");
00116       printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
00117       printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00118       printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00119       printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
00120 
00121       mp_clear(&a); mp_clear(&b); mp_clear(&c); mp_clear(&d);
00122       return 1;
00123     }
00124   }
00125   mp_clear(&d);
00126   printf("Accuracy confirmed for the %d test samples\n", num);
00127 
00128   printf("Testing Euclid ... \n");
00129   srand((unsigned int)seed);
00130   start = now();
00131   for(ix = 0; ix < num; ix++) {
00132     mpp_random_size(&a, prec);
00133     mpp_random_size(&b, prec);
00134     mp_gcd(&a, &b, &c);
00135 
00136   }
00137   finish = now();
00138 
00139   d1 = (finish.sec - start.sec) * 1000000;
00140   d1 -= start.usec; d1 += finish.usec;
00141 
00142   printf("Testing binary ... \n");
00143   srand((unsigned int)seed);
00144   start = now();
00145   for(ix = 0; ix < num; ix++) {
00146     mpp_random_size(&a, prec);
00147     mpp_random_size(&b, prec);
00148     mp_bgcd(&a, &b, &c);
00149   }
00150   finish = now();
00151 
00152   d2 = (finish.sec - start.sec) * 1000000;
00153   d2 -= start.usec; d2 += finish.usec;
00154 
00155   printf("Euclidean algorithm time: %u usec\n", d1);
00156   printf("Binary algorithm time:    %u usec\n", d2);
00157   printf("Improvement:              %.2f%%\n",
00158          (1.0 - ((double)d2 / (double)d1)) * 100.0);
00159 
00160   mp_clear(&c);
00161   mp_clear(&b);
00162   mp_clear(&a);
00163 
00164   return 0;
00165 }