Trying with a smaller lookup, still slower than std::to_string
This commit is contained in:
parent
9dbabcb5c4
commit
e3140a3add
2 changed files with 72 additions and 14 deletions
56
speed_test_int_conv.cpp
Normal file
56
speed_test_int_conv.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
#include "duckhandy/int_conv.hpp"
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
|
||||
namespace {
|
||||
template <typename Conv>
|
||||
void run_with_timing (
|
||||
const std::string& test_name,
|
||||
const std::vector<unsigned int>& src,
|
||||
std::vector<std::string>& dst,
|
||||
Conv&& conv_func
|
||||
) {
|
||||
dst.clear();
|
||||
dst.resize(src.size());
|
||||
std::cout << test_name << "...\n";
|
||||
const std::size_t count = src.size();
|
||||
|
||||
const auto t0 = std::chrono::high_resolution_clock::now();
|
||||
for (std::size_t z = 0; z < count; ++z) {
|
||||
dst[z] = conv_func(src[z]);
|
||||
}
|
||||
const auto t1 = std::chrono::high_resolution_clock::now();
|
||||
|
||||
std::chrono::duration<double> duration = t1 - t0;
|
||||
std::cout << count << " conversions completed in " << duration.count() << " seconds\n";
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
int main() {
|
||||
using dhandy::int_conv;
|
||||
constexpr const std::size_t count = 50'000'000;
|
||||
|
||||
std::vector<unsigned int> nums;
|
||||
std::vector<std::string> strings;
|
||||
std::mt19937 gen;
|
||||
|
||||
std::cout << "Setting up data...\n";
|
||||
|
||||
nums.resize(count);
|
||||
strings.reserve(count);
|
||||
gen.seed(std::time(nullptr));
|
||||
|
||||
std::cout << "Generating " << count << " random values...\n";
|
||||
std::generate(nums.begin(), nums.end(), gen);
|
||||
|
||||
run_with_timing("Conversions with std::to_string", nums, strings, [](unsigned int n) { return std::to_string(n); });
|
||||
run_with_timing("AryConversion with dhandy::int_conv", nums, strings, [](unsigned int n) { return int_conv<std::string>(n); });
|
||||
|
||||
std::cout << "All done, bye!\n";
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue