Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Simple test driver for MPI library |
michael@0 | 3 | * |
michael@0 | 4 | * Test 5a: Greatest common divisor speed test, binary vs. Euclid |
michael@0 | 5 | * |
michael@0 | 6 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | #include <string.h> |
michael@0 | 13 | #include <ctype.h> |
michael@0 | 14 | #include <limits.h> |
michael@0 | 15 | #include <time.h> |
michael@0 | 16 | |
michael@0 | 17 | #include <sys/time.h> |
michael@0 | 18 | |
michael@0 | 19 | #include "mpi.h" |
michael@0 | 20 | #include "mpprime.h" |
michael@0 | 21 | |
michael@0 | 22 | typedef struct { |
michael@0 | 23 | unsigned int sec; |
michael@0 | 24 | unsigned int usec; |
michael@0 | 25 | } instant_t; |
michael@0 | 26 | |
michael@0 | 27 | instant_t now(void) |
michael@0 | 28 | { |
michael@0 | 29 | struct timeval clk; |
michael@0 | 30 | instant_t res; |
michael@0 | 31 | |
michael@0 | 32 | res.sec = res.usec = 0; |
michael@0 | 33 | |
michael@0 | 34 | if(gettimeofday(&clk, NULL) != 0) |
michael@0 | 35 | return res; |
michael@0 | 36 | |
michael@0 | 37 | res.sec = clk.tv_sec; |
michael@0 | 38 | res.usec = clk.tv_usec; |
michael@0 | 39 | |
michael@0 | 40 | return res; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | #define PRECISION 16 |
michael@0 | 44 | |
michael@0 | 45 | int main(int argc, char *argv[]) |
michael@0 | 46 | { |
michael@0 | 47 | int ix, num, prec = PRECISION; |
michael@0 | 48 | mp_int a, b, c, d; |
michael@0 | 49 | instant_t start, finish; |
michael@0 | 50 | time_t seed; |
michael@0 | 51 | unsigned int d1, d2; |
michael@0 | 52 | |
michael@0 | 53 | seed = time(NULL); |
michael@0 | 54 | |
michael@0 | 55 | if(argc < 2) { |
michael@0 | 56 | fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]); |
michael@0 | 57 | return 1; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | if((num = atoi(argv[1])) < 0) |
michael@0 | 61 | num = -num; |
michael@0 | 62 | |
michael@0 | 63 | printf("Test 5a: Euclid vs. Binary, a GCD speed test\n\n" |
michael@0 | 64 | "Number of tests: %d\n" |
michael@0 | 65 | "Precision: %d digits\n\n", num, prec); |
michael@0 | 66 | |
michael@0 | 67 | mp_init_size(&a, prec); |
michael@0 | 68 | mp_init_size(&b, prec); |
michael@0 | 69 | mp_init(&c); |
michael@0 | 70 | mp_init(&d); |
michael@0 | 71 | |
michael@0 | 72 | printf("Verifying accuracy ... \n"); |
michael@0 | 73 | srand((unsigned int)seed); |
michael@0 | 74 | for(ix = 0; ix < num; ix++) { |
michael@0 | 75 | mpp_random_size(&a, prec); |
michael@0 | 76 | mpp_random_size(&b, prec); |
michael@0 | 77 | |
michael@0 | 78 | mp_gcd(&a, &b, &c); |
michael@0 | 79 | mp_bgcd(&a, &b, &d); |
michael@0 | 80 | |
michael@0 | 81 | if(mp_cmp(&c, &d) != 0) { |
michael@0 | 82 | printf("Error! Results not accurate:\n"); |
michael@0 | 83 | printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
michael@0 | 84 | printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
michael@0 | 85 | printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
michael@0 | 86 | printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); |
michael@0 | 87 | |
michael@0 | 88 | mp_clear(&a); mp_clear(&b); mp_clear(&c); mp_clear(&d); |
michael@0 | 89 | return 1; |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | mp_clear(&d); |
michael@0 | 93 | printf("Accuracy confirmed for the %d test samples\n", num); |
michael@0 | 94 | |
michael@0 | 95 | printf("Testing Euclid ... \n"); |
michael@0 | 96 | srand((unsigned int)seed); |
michael@0 | 97 | start = now(); |
michael@0 | 98 | for(ix = 0; ix < num; ix++) { |
michael@0 | 99 | mpp_random_size(&a, prec); |
michael@0 | 100 | mpp_random_size(&b, prec); |
michael@0 | 101 | mp_gcd(&a, &b, &c); |
michael@0 | 102 | |
michael@0 | 103 | } |
michael@0 | 104 | finish = now(); |
michael@0 | 105 | |
michael@0 | 106 | d1 = (finish.sec - start.sec) * 1000000; |
michael@0 | 107 | d1 -= start.usec; d1 += finish.usec; |
michael@0 | 108 | |
michael@0 | 109 | printf("Testing binary ... \n"); |
michael@0 | 110 | srand((unsigned int)seed); |
michael@0 | 111 | start = now(); |
michael@0 | 112 | for(ix = 0; ix < num; ix++) { |
michael@0 | 113 | mpp_random_size(&a, prec); |
michael@0 | 114 | mpp_random_size(&b, prec); |
michael@0 | 115 | mp_bgcd(&a, &b, &c); |
michael@0 | 116 | } |
michael@0 | 117 | finish = now(); |
michael@0 | 118 | |
michael@0 | 119 | d2 = (finish.sec - start.sec) * 1000000; |
michael@0 | 120 | d2 -= start.usec; d2 += finish.usec; |
michael@0 | 121 | |
michael@0 | 122 | printf("Euclidean algorithm time: %u usec\n", d1); |
michael@0 | 123 | printf("Binary algorithm time: %u usec\n", d2); |
michael@0 | 124 | printf("Improvement: %.2f%%\n", |
michael@0 | 125 | (1.0 - ((double)d2 / (double)d1)) * 100.0); |
michael@0 | 126 | |
michael@0 | 127 | mp_clear(&c); |
michael@0 | 128 | mp_clear(&b); |
michael@0 | 129 | mp_clear(&a); |
michael@0 | 130 | |
michael@0 | 131 | return 0; |
michael@0 | 132 | } |