Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ endif ()

add_executable(tinyformat_speed_test src/tinyformat-test.cc)
target_link_libraries(tinyformat_speed_test fmt ${EXTRA_LIBS})
set_target_properties(tinyformat_speed_test PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF)
if (HAVE_PROFILER)
target_link_libraries(tinyformat_speed_test ${PROFILER_LIB})
set(PROFILE_DEFS ";FMT_PROFILE")
Expand Down Expand Up @@ -161,6 +165,12 @@ else()
COMMAND @time -p ./tinyformat_speed_test format > /dev/null
COMMAND @echo fmt::compile timings:
COMMAND @time -p ./tinyformat_speed_test fmt::compile > /dev/null
COMMAND @echo fmt::format_to-string timings:
COMMAND @time -p ./tinyformat_speed_test fmt::format_to-string > /dev/null
COMMAND @echo fmt::format_to-memory_buffer timings:
COMMAND @time -p ./tinyformat_speed_test fmt::format_to-memory_buffer > /dev/null
COMMAND @echo fmt::format_to-vector_char timings:
COMMAND @time -p ./tinyformat_speed_test fmt::format_to-vector_char > /dev/null
COMMAND @echo tinyformat timings:
COMMAND @time -p ./tinyformat_speed_test tinyformat > /dev/null
COMMAND @echo boost timings:
Expand Down
28 changes: 28 additions & 0 deletions src/tinyformat-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace std { class type_info; }
#include <cstddef>

#ifdef SPEED_TEST
#include <iterator>
#ifdef HAVE_FORMAT
# include "fmt/format.h"
# include "fmt/compile.h"
Expand All @@ -26,6 +27,9 @@ namespace std { class type_info; }
#include "stb_sprintf.h"
#include <iomanip>
#include <stdio.h>
#include <string>
#include <string_view>
#include <vector>
#endif

// Throw instead of abort() so we can test error conditions.
Expand Down Expand Up @@ -129,6 +133,30 @@ void speedTest(const std::string& which)
std::puts(buf);
}
}
else if(which == "fmt::format_to-string" ||
which == "fmt::format_to-memory_buffer" ||
which == "fmt::format_to-vector_char")
{
const int x = 42;
const double y = 3.14159;
const std::string s(3000, 'x');
const auto bench = [&](auto&& make) {
std::string out;
for(long i = 0; i < maxIter; ++i)
{
auto b = make();
fmt::format_to(std::back_inserter(b), "{} {} {}", x, y, s);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not equivalent to other cases.

out.assign(b.data(), b.size());
}
std::cout << out << "\n";
};
if(which == "fmt::format_to-string")
bench([] { return std::string(); });
else if(which == "fmt::format_to-memory_buffer")
bench([] { return fmt::memory_buffer(); });
else
bench([] { return std::vector<char>(); });
}
#endif
else if(which == "folly")
{
Expand Down