Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Speed comparison of a BigNumber class vs GMP
- https://github.com/pr0crustes/BigNumber
- g++ -O3 -Wall -Wno-sign-compare -Iinclude mult_speed.cc -lgmpxx -lgmp -o mult_speed
- Time to multiply two N-digit numbers, in seconds
- 1000 2000 10000000 20000000
- BigNumber 2.19 15.1
- GMP .0000223 .0000268 .33 .67
- */
- #include <iostream>
- #include <string>
- #include <random>
- #include <chrono>
- #include <cstdlib>
- #include <gmpxx.h>
- #include "BigNumber.h"
- typedef std::chrono::high_resolution_clock::time_point TimeType;
- std::random_device rand_dev;
- std::default_random_engine rand_engine(rand_dev());
- std::uniform_int_distribution<int> rand_int_0_9(0, 9), rand_int_1_9(1, 9);
- TimeType now() {
- return std::chrono::high_resolution_clock::now();
- }
- double timeDiff(const TimeType &start_time, const TimeType &end_time) {
- typedef std::chrono::duration<double> seconds_type;
- return std::chrono::duration_cast<seconds_type>(end_time - start_time)
- .count();
- }
- void randomFill(std::string &s) {
- if (s.length() == 0) return;
- s[0] = '0' + rand_int_1_9(rand_engine);
- for (size_t i=1; i < s.length(); i++)
- s[i] = '0' + rand_int_0_9(rand_engine);
- }
- void testBigNumber(const std::string &in_str1, const std::string &in_str2) {
- TimeType start_time;
- std::cout << "Convert to BigNumber..." << std::flush;
- start_time = now();
- BigNumber bignum_in1(in_str1), bignum_in2(in_str2);
- std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
- BigNumber bignum_out;
- std::cout << "Multiplying BigNumber..." << std::flush;
- start_time = now();
- bignum_out = bignum_in1 * bignum_in2;
- std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
- // std::cout << bignum_out << std::endl;
- // make sure the multiplication isn't optimized out
- if (bignum_out.isZero()) std::cout << "(shouldn't output)\n";
- std::cout << std::endl;
- }
- void testGMP(const std::string &in_str1, const std::string &in_str2) {
- TimeType start_time;
- std::cout << "Converting to mpz_class..." << std::flush;
- start_time = now();
- mpz_class gmp_in1(in_str1), gmp_in2(in_str2);
- std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
- // std::cout << gmp_in1 << std::endl;
- // std::cout << gmp_in2 << std::endl;
- std::cout << "Multiplying mpz_class..." << std::flush;
- mpz_class gmp_out;
- start_time = now();
- gmp_out = gmp_in1 * gmp_in2;
- std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
- // std::cout << gmp_out << std::endl;
- // make sure the multiplication isn't optimized out
- if (sgn(gmp_out) == -1) std::cout << "(shouldn't output)\n";
- std::cout << std::endl;
- }
- int main(int argc, char **argv) {
- TimeType start_time;
- int length = 10;
- if (argc == 1) {
- std::cout << "\n mult_speed <length>\n"
- << " Time BigNumber and GMP multiply two integers of <length> digits.\n\n";
- return 0;
- } else {
- length = atoi(argv[1]);
- if (length <= 0) {
- std::cout << "Length must be positive.\n";
- return 1;
- }
- }
- std::cout << "Length=" << length << std::endl;
- std::string in_str1(length, '0'), in_str2(length, '0');
- std::cout << "Creating random inputs..." << std::flush;
- start_time = now();
- randomFill(in_str1);
- randomFill(in_str2);
- std::cout << "done (" << timeDiff(start_time, now()) << "s)\n" << std::flush;
- // std::cout << in_str1 << std::endl;
- // std::cout << in_str2 << std::endl;
- std::cout << std::endl;
- testBigNumber(in_str1, in_str2);
- testGMP(in_str1, in_str2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement