Advertisement
pi_stuff

Performance comparison pr0crustes/BigNumber vs GMP

Nov 10th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. /*
  2.   Speed comparison of a BigNumber class vs GMP
  3.   https://github.com/pr0crustes/BigNumber
  4.  
  5.   g++ -O3 -Wall -Wno-sign-compare -Iinclude mult_speed.cc -lgmpxx -lgmp -o mult_speed
  6.  
  7.            Time to multiply two N-digit numbers, in seconds
  8.                    1000      2000  10000000 20000000
  9.     BigNumber      2.19      15.1
  10.     GMP        .0000223  .0000268       .33      .67
  11. */
  12. #include <iostream>
  13. #include <string>
  14. #include <random>
  15. #include <chrono>
  16. #include <cstdlib>
  17. #include <gmpxx.h>
  18. #include "BigNumber.h"
  19.  
  20. typedef std::chrono::high_resolution_clock::time_point TimeType;
  21.  
  22. std::random_device rand_dev;
  23. std::default_random_engine rand_engine(rand_dev());
  24. std::uniform_int_distribution<int> rand_int_0_9(0, 9), rand_int_1_9(1, 9);
  25.  
  26. TimeType now() {
  27.   return std::chrono::high_resolution_clock::now();
  28. }
  29.  
  30. double timeDiff(const TimeType &start_time, const TimeType &end_time) {
  31.   typedef std::chrono::duration<double> seconds_type;
  32.   return std::chrono::duration_cast<seconds_type>(end_time - start_time)
  33.     .count();
  34. }
  35.  
  36. void randomFill(std::string &s) {
  37.   if (s.length() == 0) return;
  38.   s[0] = '0' + rand_int_1_9(rand_engine);
  39.   for (size_t i=1; i < s.length(); i++)
  40.     s[i] = '0' + rand_int_0_9(rand_engine);
  41. }
  42.  
  43.  
  44. void testBigNumber(const std::string &in_str1, const std::string &in_str2) {
  45.   TimeType start_time;
  46.  
  47.   std::cout << "Convert to BigNumber..." << std::flush;
  48.   start_time = now();
  49.   BigNumber bignum_in1(in_str1), bignum_in2(in_str2);
  50.   std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
  51.  
  52.   BigNumber bignum_out;
  53.  
  54.   std::cout << "Multiplying BigNumber..." << std::flush;
  55.   start_time = now();
  56.   bignum_out = bignum_in1 * bignum_in2;
  57.   std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
  58.  
  59.   // std::cout << bignum_out << std::endl;
  60.  
  61.   // make sure the multiplication isn't optimized out
  62.   if (bignum_out.isZero()) std::cout << "(shouldn't output)\n";
  63.  
  64.   std::cout << std::endl;
  65. }
  66.  
  67.  
  68. void testGMP(const std::string &in_str1, const std::string &in_str2) {
  69.   TimeType start_time;
  70.  
  71.   std::cout << "Converting to mpz_class..." << std::flush;
  72.   start_time = now();
  73.   mpz_class gmp_in1(in_str1), gmp_in2(in_str2);
  74.   std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
  75.   // std::cout << gmp_in1 << std::endl;
  76.   // std::cout << gmp_in2 << std::endl;
  77.  
  78.   std::cout << "Multiplying mpz_class..." << std::flush;
  79.   mpz_class gmp_out;
  80.   start_time = now();
  81.   gmp_out = gmp_in1 * gmp_in2;
  82.  
  83.   std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
  84.  
  85.   // std::cout << gmp_out << std::endl;
  86.  
  87.   // make sure the multiplication isn't optimized out
  88.   if (sgn(gmp_out) == -1) std::cout << "(shouldn't output)\n";
  89.  
  90.   std::cout << std::endl;
  91. }
  92.  
  93.  
  94. int main(int argc, char **argv) {
  95.   TimeType start_time;
  96.   int length = 10;
  97.  
  98.   if (argc == 1) {
  99.     std::cout << "\n  mult_speed <length>\n"
  100.               << "  Time BigNumber and GMP multiply two integers of <length> digits.\n\n";
  101.     return 0;
  102.   } else {
  103.     length = atoi(argv[1]);
  104.     if (length <= 0) {
  105.       std::cout << "Length must be positive.\n";
  106.       return 1;
  107.     }
  108.   }
  109.  
  110.   std::cout << "Length=" << length << std::endl;
  111.  
  112.   std::string in_str1(length, '0'), in_str2(length, '0');
  113.   std::cout << "Creating random inputs..." << std::flush;
  114.   start_time = now();
  115.   randomFill(in_str1);
  116.   randomFill(in_str2);
  117.   std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
  118.   // std::cout << in_str1 << std::endl;
  119.   // std::cout << in_str2 << std::endl;
  120.  
  121.   std::cout << std::endl;
  122.  
  123.   testBigNumber(in_str1, in_str2);
  124.   testGMP(in_str1, in_str2);
  125.  
  126.   return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement