michael@0: /* michael@0: * Simple test driver for MPI library michael@0: * michael@0: * Test 3a: Multiplication vs. squaring timing test michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: #include "mpi.h" michael@0: #include "mpprime.h" michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: int ix, num, prec = 8; michael@0: double d1, d2; michael@0: clock_t start, finish; michael@0: time_t seed; michael@0: mp_int a, c, d; michael@0: michael@0: seed = time(NULL); michael@0: michael@0: if(argc < 2) { michael@0: fprintf(stderr, "Usage: %s []\n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: if((num = atoi(argv[1])) < 0) michael@0: num = -num; michael@0: michael@0: if(!num) { michael@0: fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: if(argc > 2) { michael@0: if((prec = atoi(argv[2])) <= 0) michael@0: prec = 8; michael@0: else michael@0: prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT; michael@0: } michael@0: michael@0: printf("Test 3a: Multiplication vs squaring timing test\n" michael@0: "Precision: %d digits (%u bits)\n" michael@0: "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num); michael@0: michael@0: mp_init_size(&a, prec); michael@0: michael@0: mp_init(&c); mp_init(&d); michael@0: michael@0: printf("Verifying accuracy ... \n"); michael@0: srand((unsigned int)seed); michael@0: for(ix = 0; ix < num; ix++) { michael@0: mpp_random_size(&a, prec); michael@0: mp_mul(&a, &a, &c); michael@0: mp_sqr(&a, &d); michael@0: michael@0: if(mp_cmp(&c, &d) != 0) { michael@0: printf("Error! Results not accurate:\n"); michael@0: printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); michael@0: printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); michael@0: printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); michael@0: mp_sub(&c, &d, &d); michael@0: printf("dif "); mp_print(&d, stdout); fputc('\n', stdout); michael@0: mp_clear(&c); mp_clear(&d); michael@0: mp_clear(&a); michael@0: return 1; michael@0: } michael@0: } michael@0: printf("Accuracy is confirmed for the %d test samples\n", num); michael@0: mp_clear(&d); michael@0: michael@0: printf("Testing squaring ... \n"); michael@0: srand((unsigned int)seed); michael@0: start = clock(); michael@0: for(ix = 0; ix < num; ix++) { michael@0: mpp_random_size(&a, prec); michael@0: mp_sqr(&a, &c); michael@0: } michael@0: finish = clock(); michael@0: michael@0: d2 = (double)(finish - start) / CLOCKS_PER_SEC; michael@0: michael@0: printf("Testing multiplication ... \n"); michael@0: srand((unsigned int)seed); michael@0: start = clock(); michael@0: for(ix = 0; ix < num; ix++) { michael@0: mpp_random(&a); michael@0: mp_mul(&a, &a, &c); michael@0: } michael@0: finish = clock(); michael@0: michael@0: d1 = (double)(finish - start) / CLOCKS_PER_SEC; michael@0: michael@0: printf("Multiplication time: %.3f sec (%.3f each)\n", d1, d1 / num); michael@0: printf("Squaring time: %.3f sec (%.3f each)\n", d2, d2 / num); michael@0: printf("Improvement: %.2f%%\n", (1.0 - (d2 / d1)) * 100.0); michael@0: michael@0: mp_clear(&c); michael@0: mp_clear(&a); michael@0: michael@0: return 0; michael@0: }