Last active
May 2, 2024 09:00
-
-
Save afabri/db497cd574b959c110f0a6adbfefb9f7 to your computer and use it in GitHub Desktop.
Speed difference between mpz_int and cpp_int for function convert_to<std::string>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/multiprecision/cpp_int.hpp> | |
#include <boost/multiprecision/gmp.hpp> | |
#include <chrono> | |
#include <iostream> | |
#include <string> | |
template <typename Number> | |
void fct(const std::string& forth) | |
{ | |
const auto start{ std::chrono::steady_clock::now() }; | |
Number big_int(forth); | |
std::string back = big_int. template convert_to<std::string>(); | |
const auto end{ std::chrono::steady_clock::now() }; | |
if (forth.length() != back.length()) { | |
std::cerr << "error" << std::endl; | |
} | |
const std::chrono::duration<double> elapsed_seconds{ end - start }; | |
std::cout << elapsed_seconds.count() << " sec." << std::endl; | |
} | |
int main() | |
{ | |
using namespace boost::multiprecision; | |
std::string ten("1234567890"); | |
std::string forth; | |
for (int i = 0; i < 10000; ++i) { | |
forth += ten; | |
} | |
std::cout << "number of digits: " << forth.length() << std::endl; | |
fct<mpz_int>(forth); | |
fct<cpp_int>(forth); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment