mirror of
https://github.com/fmtlib/fmt.git
synced 2024-09-03 05:29:16 +00:00
Add format to int_generator tests.
This commit is contained in:
parent
717a8a9514
commit
75343c7e12
@ -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
|
||||
|
4
format.h
4
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]; }
|
||||
|
@ -10,7 +10,9 @@
|
||||
#include <sstream>
|
||||
#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
|
||||
// Karma.
|
||||
@ -48,10 +50,10 @@ int main()
|
||||
std::vector<int> 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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user