Skip to content

Instantly share code, notes, and snippets.

@DBJDBJ
Created November 6, 2019 15:31
Show Gist options
  • Save DBJDBJ/0d9cc7ced343f6024468b22a9f98ccf6 to your computer and use it in GitHub Desktop.
Save DBJDBJ/0d9cc7ced343f6024468b22a9f98ccf6 to your computer and use it in GitHub Desktop.
testing {fmt} fmt::format_to() vs the buffer used
/*
(c) 2019/2020 by [email protected], LICENCE CC BY SA 4.0
Compiled using Visual Studio 2019 with CLANG 8.0.1
Machine Win10 PRO, CPU i5, 8 GB RAM
-----------------------------------------------------------
Typical Results:
-----------------------------------------------------------
CLANG 8.0.1 (tags/RELEASE_801/final),32 bit, Release build
Iterating 65535 times.
Narrow buffers-------------------------------------------
using char [bufsiz], format_to() total time is: 0.006 seconds
using std::array<char, bufsiz>, format_to() total time is: 0.006 seconds
using fmt::memory_buffer, format_to() total time is: 0.006 seconds
using std::unique_ptr<char[]>, format_to() total time is: 0.012 seconds
using std::vector<char>, format_to() total time is: 0.012 seconds
using std::string, format_to() total time is: 0.013 seconds
Wide buffers-------------------------------------------
using wchar_t [bufsiz], format_to() total time is: 0.006 seconds
using std::array<wchar_t, bufsiz>, format_to() total time is: 0.006 seconds
using fmt::wmemory_buffer, format_to() total time is: 0.006 seconds
using std::unique_ptr<wchar_t[]>, format_to() total time is: 0.012 seconds
using std::vector<wchar_t>, format_to() total time is: 0.012 seconds
using std::wstring, format_to() total time is: 0.012 seconds
Done..
*/
#include "common.h"
namespace {
constexpr size_t number_of_iterations = size_t(0xFFFF);
constexpr size_t bufsiz = size_t(64);
inline std::random_device random_device_;
inline std::mt19937 the_generator_(random_device_());
inline std::uniform_int_distribution<> from_25_to_64_(25, 63);
}
template< typename T>
inline typename T::iterator dbj_begin(T& t_) noexcept { return t_.begin(); }
template< typename T>
inline T* dbj_begin(fmt::basic_memory_buffer<T>& t_) noexcept { return t_.begin(); }
template< typename T>
inline T* dbj_begin(std::unique_ptr<T[]>& t_) noexcept { return t_.get(); }
template< typename T>
inline T* dbj_begin( T nat_[]) noexcept { return nat_ ; }
extern "C" int test_1(int, wchar_t**)
{
#ifdef __clang__
std::printf("\n\nCLANG %s", __clang_version__);
#else
std::printf("\n\nMSVC %d", _MSVC_LANG);
#endif
#ifdef _WIN64
std::printf(",64 bit");
#else
std::printf(",32 bit");
#endif
#ifdef NDEBUG
std::printf(", Release build\n");
#else
std::printf(", Debug build\n");
#endif
auto driver = [](char const* prompt_, auto test_, auto buf_maker_) {
size_t ctr_ = 0;
std::printf("\n%s", prompt_);
const int random_number = from_25_to_64_(the_generator_);
time_t st = clock();
while (ctr_++ < number_of_iterations) {
test_(buf_maker_, random_number);
}
const float rezult = double((clock() - st)) / CLOCKS_PER_SEC;
std::printf(", format_to() total time is: %.3f seconds\n", rezult );
};
std::printf("\nIterating %zu times.\n", number_of_iterations);
auto test_unit_ = [](auto buf_maker_, int number_) {
auto mb_ = buf_maker_();
[[maybe_unused]] auto rezult = fmt::format_to(dbj_begin(mb_), "The answer is {}.", number_);
};
std::printf("\nNarrow buffers-------------------------------------------\n");
char narrow_native[bufsiz]{0};;
driver("using char [bufsiz]", test_unit_, [&]() -> char (&)[bufsiz] { return narrow_native; });
driver("using std::array<char, bufsiz>", test_unit_, []() { return std::array<char, bufsiz>{ {0} }; });
driver("using fmt::memory_buffer", test_unit_, []() {return fmt::memory_buffer{}; });
driver("using std::unique_ptr<char[]>", test_unit_, []() { return std::make_unique<char[]>(bufsiz); });
driver("using std::vector<char>", test_unit_, []() { return std::vector<char>(bufsiz, char(0)); });
driver("using std::string", test_unit_, []() { return std::string(bufsiz, char(0)); });
std::printf("\nWide buffers-------------------------------------------\n");
wchar_t wide_native[bufsiz]{ 0 };;
driver("using wchar_t [bufsiz]", test_unit_, [&]() -> wchar_t(&)[bufsiz] { return wide_native; });
driver("using std::array<wchar_t, bufsiz>", test_unit_, []() { return std::array<wchar_t, bufsiz>{ {0} }; });
driver("using fmt::wmemory_buffer", test_unit_, []() { return fmt::wmemory_buffer{}; });
driver("using std::unique_ptr<wchar_t[]>", test_unit_, []() { return std::make_unique<wchar_t[]>(bufsiz); });
driver("using std::vector<wchar_t>", test_unit_, []() { return std::vector<wchar_t>(bufsiz, wchar_t(0)); });
driver("using std::wstring", test_unit_, []() { return std::wstring(bufsiz, wchar_t(0)); });
std::printf("\nDone..\n");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment