From 79dc9edc72c5b9fefb129fe36029d4781b1e969c Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 25 Dec 2021 14:38:46 +0100 Subject: Generalized type usage and optimizations --- src/test-unicode.cpp | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/test-unicode.cpp b/src/test-unicode.cpp index d638cbb..a30be70 100644 --- a/src/test-unicode.cpp +++ b/src/test-unicode.cpp @@ -1,11 +1,11 @@ #define BOOST_TEST_MODULE unicode_test +#include #include #include #include #include - -#include +#include #include #include @@ -24,6 +24,7 @@ #include using namespace std::chrono_literals; +using namespace std::string_literals; typedef std::tuple, std::basic_string, std::basic_string> types_collection_type; @@ -442,6 +443,27 @@ namespace { } } +class CPUTimer +{ +public: + CPUTimer(const std::string& name = "Timer"): mName(name), mWallTime0(std::chrono::steady_clock::now()) + { + } + + ~CPUTimer() + { + auto elapsed_cpu{mCPUTimer.elapsed()}; + std::cout << mName << ": " << std::chrono::duration(std::chrono::steady_clock::now() - mWallTime0).count() << + "s (" << (double(elapsed_cpu.user + elapsed_cpu.system) / 1000000000) << + "s CPU)" << std::endl; + } + +private: + std::string mName; + std::chrono::time_point mWallTime0; + boost::timer::cpu_timer mCPUTimer; +}; + template void test_random_valid(random_context& rc, size_t length, const std::string& description) { @@ -466,33 +488,24 @@ void test_random_valid(random_context& rc, size_t length, const std::string& des } { - auto t0{std::chrono::steady_clock::now()}; + CPUTimer timer("Performance test for converting "s + std::to_string(list.size()) + + " "s + description + + " from UTF-"s + std::to_string(sizeof(typename From::value_type) * 8) + + " to UTF-"s + std::to_string(sizeof(typename To::value_type) * 8)); for (const auto& i: list) To result{unicode::convert::Facet,typename unicode::Encoding::Facet>(i)}; - std::cout << "Performance test for converting " << list.size() << - " " << description << - " from UTF-" << (sizeof(typename From::value_type) * 8) << - " to UTF-" << (sizeof(typename To::value_type) * 8) << ": " << - std::chrono::duration(std::chrono::steady_clock::now() - t0).count() << "s" << - std::endl; } { - auto t0{std::chrono::steady_clock::now()}; + CPUTimer timer(" -> Compare to boost::locale::conv::utf_to_utf"); for (const auto& i: list) To result{boost::locale::conv::utf_to_utf(i)}; - std::cout << " -> Compare to boost::locale::conv::utf_to_utf: " << - std::chrono::duration(std::chrono::steady_clock::now() - t0).count() << "s" << - std::endl; } { - auto t0{std::chrono::steady_clock::now()}; + CPUTimer timer(" -> Compare to std::wstring_convert"); for (const auto& i: list) To result{std_convert(i)}; - std::cout << " -> Compare to std::wstring_convert: " << - std::chrono::duration(std::chrono::steady_clock::now() - t0).count() << "s" << - std::endl; } // iterate over remaining To types @@ -615,6 +628,10 @@ BOOST_AUTO_TEST_CASE(string_u8string) a = std::string{b.begin(), b.end()}; BOOST_CHECK(a == std::string{"\xc3\xa4"}); + + BOOST_CHECK(sizeof(size_t) == 4 || sizeof(size_t) == 8); + + std::cout << "Detected CPU Accu size: " << (sizeof(size_t) * 8) << std::endl; } // check environment: demonstrate how boost convert u8->u8 throws exception on invalid input -- cgit v1.2.3