duckhandy/speed_test_int_conv.cpp
2021-06-07 20:21:32 +02:00

56 lines
1.5 KiB
C++

#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 = 70'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;
}