// -*- C++ -*- // Boost general library 'format' --------------------------- // See http://www.boost.org for updates, documentation, and revision history. // (C) Samuel Krempp 2001 // krempp@crans.ens-cachan.fr // Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // several suggestions from Jens Maurer // ------------------------------------------------------------------------------ // bench_variants.cc : do the same task, with snprintf, stream, and format // and compare their times. // This benchmark is provided purely for information. // It might not even compile as-is, // or not give any sensible results. // (e.g., it expects sprintf to be POSIX compliant) // ------------------------------------------------------------------------------ #include #include #include // sprintf #include #include #include // floor #include #include "boost/format.hpp" //#define knelson #ifdef knelson namespace KNelson { #include "boost/format3.hpp" } #endif // portable /dev/null stream equivalent, by James Kanze, http://www.gabi-soft.de class NulStreambuf : public std::streambuf { public: NulStreambuf() { setp( dummyBuffer , dummyBuffer + 64 ) ; } virtual int overflow( int c ); virtual int underflow(); private: char dummyBuffer[ 64 ] ; } ; class NulStream : public std::basic_ostream > { public: NulStream(); virtual ~NulStream(); NulStreambuf* rdbuf() { return static_cast< NulStreambuf* >( ((std::basic_ostream > *) this) -> rdbuf() ) ; } } ; //------------------------------------------------------------------------------------- // NulStream implementation NulStream::NulStream() : std::basic_ostream > (NULL) { init( new NulStreambuf ) ; } NulStream::~NulStream() { delete rdbuf() ; } int NulStreambuf::underflow(){ return std::ios::traits_type::eof(); } int NulStreambuf::overflow( int c ){ setp( dummyBuffer , dummyBuffer + 64 ) ; return (c == std::ios::traits_type::eof()) ? '\0' : c ; } // ------------------------------------------------------------------------------------- static int NTests = 300000; //static std::stringstream nullStream; static NulStream nullStream; static double tstream, tpf; //static const std::string fstring="%3$#x %1$20.10E %2$g %3$d \n"; static const std::string fstring="%3$0#6x %1$20.10E %2$g %3$0+5d \n"; static const double arg1=45.23; static const double arg2=12.34; static const int arg3=23; static const std::string res = "0x0017 4.5230000000E+01 12.34 +0023 \n"; //static const std::string res = "23.0000 4.5230000000E+01 12.34 23 \n"; void test_snprintf(); void test_nullstream(); void test_opti_nullstream(); void test_parsed_once_format(); void test_reused_format(); void test_format(); void test_try1(); void test_try2(); #ifdef knelson void test_format3(); #endif int main(int argc, char * argv[]) { using namespace boost; using namespace std; const string::size_type npos = string::npos; string choices=""; if(1