diff --git a/CMakeLists.txt b/CMakeLists.txt index e679b122..c45e8811 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,12 +40,18 @@ if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt) add_test(format_test format_test) endif () -if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tinyformat/tinyformat_test.cpp) - find_package(Boost) - if (Boost_FOUND) - add_definitions(-DHAVE_BOOST) +find_package(Boost) +if (Boost_FOUND) + add_executable(int_generator tests/int_generator.cpp) + target_link_libraries(int_generator format) + find_library(HAVE_RT rt) + if (HAVE_RT) + target_link_libraries(int_generator rt) endif () + add_definitions(-DHAVE_BOOST) +endif () +if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tinyformat/tinyformat_test.cpp) add_executable(tinyformat_speed_test tinyformat/tinyformat_test.cpp) target_link_libraries(tinyformat_speed_test format) set_target_properties(tinyformat_speed_test PROPERTIES COMPILE_DEFINITIONS diff --git a/format.h b/format.h index d128a8a8..23daee78 100644 --- a/format.h +++ b/format.h @@ -306,6 +306,10 @@ class Formatter { // using inserter operator<<. internal::ArgInserter operator()(const char *format); + void operator<<(int value) { + FormatInt(value, FormatSpec()); + } + std::size_t size() const { return buffer_.size(); } const char *data() const { return &buffer_[0]; } diff --git a/tests/int_generator.cpp b/tests/int_generator.cpp index 7c822a2c..8c5474f0 100644 --- a/tests/int_generator.cpp +++ b/tests/int_generator.cpp @@ -10,7 +10,9 @@ #include #include -#include "../high_resolution_timer.hpp" +#include "high_resolution_timer.hpp" + +#include "../format.h" // This value specifies, how to unroll the integer string generation loop in // Karma. @@ -48,10 +50,10 @@ int main() std::vector v (MAX_ITERATION); std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector - // test the C libraries ltoa function (the most low level function for + // test the C libraries sprintf function (the most low level function for // string conversion available) { - //[karma_int_performance_ltoa + //[karma_int_performance_sprintf char buffer[65]; // we don't expect more than 64 bytes to be generated here //<- std::string str; @@ -59,14 +61,14 @@ int main() //-> for (int i = 0; i < MAX_ITERATION; ++i) { - ltoa(v[i], buffer, 10); + sprintf(buffer, "%d", v[i]); //<- str = buffer; // compensate for string ops in other benchmarks //-> } //] - cout << "ltoa:\t\t" << t.elapsed() << " [s]" << flush << endl; + cout << "sprintf:\t\t" << t.elapsed() << " [s]" << flush << endl; } // test the iostreams library @@ -88,7 +90,7 @@ int main() // test the Boost.Format library { - //[karma_int_performance_format + //[karma_int_performance_boost std::string str; boost::format int_format("%d"); //<- @@ -124,6 +126,25 @@ int main() cout << "int_:\t\t" << t.elapsed() << " [s]" << flush << endl; } + // test the format library + { + std::string str; + util::high_resolution_timer t; + + //[karma_int_performance_format + for (int i = 0; i < MAX_ITERATION; ++i) + { + fmt::Formatter format; + format << v[i]; + //<- + str = format.c_str(); // compensate for string ops in other benchmarks + //-> + } + //] + + cout << "format:\t\t" << t.elapsed() << " [s]" << flush << endl; + } + return 0; }