mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-02 20:32:49 +00:00
152 lines
4.0 KiB
C++
152 lines
4.0 KiB
C++
// Copyright (c) 2001-2010 Hartmut Kaiser
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
#include <climits>
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <boost/format.hpp>
|
|
|
|
#include "high_resolution_timer.hpp"
|
|
|
|
#include "../format.h"
|
|
|
|
// This value specifies, how to unroll the integer string generation loop in
|
|
// Karma.
|
|
// Set this to some integer in between 0 (no unrolling) and max expected
|
|
// integer string len (complete unrolling). If not specified, this value
|
|
// defaults to 6.
|
|
#define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
|
|
|
|
#include <boost/spirit/include/karma.hpp>
|
|
|
|
using namespace std;
|
|
using namespace boost::spirit;
|
|
|
|
#define MAX_ITERATION 10000000
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
struct random_fill
|
|
{
|
|
int operator()() const
|
|
{
|
|
int scale = std::rand() / 100 + 1;
|
|
return (std::rand() * std::rand()) / scale;
|
|
}
|
|
};
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
int main()
|
|
{
|
|
namespace karma = boost::spirit::karma;
|
|
|
|
cout << "Converting " << MAX_ITERATION
|
|
<< " randomly generated int values to strings." << flush << endl;
|
|
|
|
std::srand(0);
|
|
std::vector<int> v (MAX_ITERATION);
|
|
std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector
|
|
|
|
// test the C libraries sprintf function (the most low level function for
|
|
// string conversion available)
|
|
{
|
|
//[karma_int_performance_sprintf
|
|
char buffer[65]; // we don't expect more than 64 bytes to be generated here
|
|
//<-
|
|
std::string str;
|
|
util::high_resolution_timer t;
|
|
//->
|
|
for (int i = 0; i < MAX_ITERATION; ++i)
|
|
{
|
|
sprintf(buffer, "%d", v[i]);
|
|
//<-
|
|
str = buffer; // compensate for string ops in other benchmarks
|
|
//->
|
|
}
|
|
//]
|
|
|
|
cout << "sprintf:\t\t" << t.elapsed() << " [s]" << flush << endl;
|
|
}
|
|
|
|
// test the iostreams library
|
|
{
|
|
//[karma_int_performance_iostreams
|
|
std::stringstream str;
|
|
//<-
|
|
util::high_resolution_timer t;
|
|
//->
|
|
for (int i = 0; i < MAX_ITERATION; ++i)
|
|
{
|
|
str.str("");
|
|
str << v[i];
|
|
}
|
|
//]
|
|
|
|
cout << "iostreams:\t" << t.elapsed() << " [s]" << flush << endl;
|
|
}
|
|
|
|
// test the Boost.Format library
|
|
{
|
|
//[karma_int_performance_boost
|
|
std::string str;
|
|
boost::format int_format("%d");
|
|
//<-
|
|
util::high_resolution_timer t;
|
|
//->
|
|
for (int i = 0; i < MAX_ITERATION; ++i)
|
|
{
|
|
str = boost::str(int_format % v[i]);
|
|
}
|
|
//]
|
|
|
|
cout << "Boost.Format:\t" << t.elapsed() << " [s]" << flush << endl;
|
|
}
|
|
|
|
// test the Karma int_ generation routines
|
|
{
|
|
std::string str;
|
|
util::high_resolution_timer t;
|
|
|
|
//[karma_int_performance_plain
|
|
char buffer[65]; // we don't expect more than 64 bytes to be generated here
|
|
for (int i = 0; i < MAX_ITERATION; ++i)
|
|
{
|
|
char *ptr = buffer;
|
|
karma::generate(ptr, int_, v[i]);
|
|
*ptr = '\0';
|
|
//<-
|
|
str = buffer; // compensate for string ops in other benchmarks
|
|
//->
|
|
}
|
|
//]
|
|
|
|
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
|
|
fmt::Writer writer;
|
|
for (int i = 0; i < MAX_ITERATION; ++i)
|
|
{
|
|
writer.Clear();
|
|
writer << v[i];
|
|
//<-
|
|
str = writer.c_str(); // compensate for string ops in other benchmarks
|
|
//->
|
|
}
|
|
//]
|
|
|
|
cout << "format:\t\t" << t.elapsed() << " [s]" << flush << endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|