Commit 75343c7e authored by Victor Zverovich's avatar Victor Zverovich

Add format to int_generator tests.

parent 717a8a95
...@@ -40,12 +40,18 @@ if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt) ...@@ -40,12 +40,18 @@ if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt)
add_test(format_test format_test) add_test(format_test format_test)
endif () endif ()
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/tinyformat/tinyformat_test.cpp) find_package(Boost)
find_package(Boost) if (Boost_FOUND)
if (Boost_FOUND) add_executable(int_generator tests/int_generator.cpp)
add_definitions(-DHAVE_BOOST) target_link_libraries(int_generator format)
find_library(HAVE_RT rt)
if (HAVE_RT)
target_link_libraries(int_generator rt)
endif () 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) add_executable(tinyformat_speed_test tinyformat/tinyformat_test.cpp)
target_link_libraries(tinyformat_speed_test format) target_link_libraries(tinyformat_speed_test format)
set_target_properties(tinyformat_speed_test PROPERTIES COMPILE_DEFINITIONS set_target_properties(tinyformat_speed_test PROPERTIES COMPILE_DEFINITIONS
......
...@@ -306,6 +306,10 @@ class Formatter { ...@@ -306,6 +306,10 @@ class Formatter {
// using inserter operator<<. // using inserter operator<<.
internal::ArgInserter operator()(const char *format); internal::ArgInserter operator()(const char *format);
void operator<<(int value) {
FormatInt(value, FormatSpec());
}
std::size_t size() const { return buffer_.size(); } std::size_t size() const { return buffer_.size(); }
const char *data() const { return &buffer_[0]; } const char *data() const { return &buffer_[0]; }
......
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
#include <sstream> #include <sstream>
#include <boost/format.hpp> #include <boost/format.hpp>
#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 // This value specifies, how to unroll the integer string generation loop in
// Karma. // Karma.
...@@ -48,10 +50,10 @@ int main() ...@@ -48,10 +50,10 @@ int main()
std::vector<int> v (MAX_ITERATION); std::vector<int> v (MAX_ITERATION);
std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector 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) // 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 char buffer[65]; // we don't expect more than 64 bytes to be generated here
//<- //<-
std::string str; std::string str;
...@@ -59,14 +61,14 @@ int main() ...@@ -59,14 +61,14 @@ int main()
//-> //->
for (int i = 0; i < MAX_ITERATION; ++i) 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 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 // test the iostreams library
...@@ -88,7 +90,7 @@ int main() ...@@ -88,7 +90,7 @@ int main()
// test the Boost.Format library // test the Boost.Format library
{ {
//[karma_int_performance_format //[karma_int_performance_boost
std::string str; std::string str;
boost::format int_format("%d"); boost::format int_format("%d");
//<- //<-
...@@ -124,6 +126,25 @@ int main() ...@@ -124,6 +126,25 @@ int main()
cout << "int_:\t\t" << t.elapsed() << " [s]" << flush << endl; 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; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment