// several suggestions from Jens Maurer
// ------------------------------------------------------------------------------
-// bench_variants.cc : do the same task, with snprintf, stream, and format
+// bench_variants.cc : do the same task, with sprintf, stream, and format
// and compare their times.
// This benchmark is provided purely for information.
#include <fstream>
#include <cmath> // floor
#include <boost/timer.hpp>
+#include <vector>
#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
{
// -------------------------------------------------------------------------------------
+namespace benchmark {
+
static int NTests = 300000;
//static std::stringstream nullStream;
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_sprintf();
void test_nullstream();
void test_opti_nullstream();
void test_parsed_once_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<argc) {
- choices = (argv[1]); // profiling is easier launching only one.
- NTests = 1000*1000; // andmoreprecise with many iterations
- cout << "choices (" << choices << ") \n";
- }
-
- if(choices=="" || choices.find('p') !=npos)
- test_snprintf();
- if(choices=="" || choices.find('n') !=npos)
- test_nullstream();
- if(choices=="" || choices.find('1') !=npos)
- test_parsed_once_format();
- if(choices=="" || choices.find('r') !=npos)
- test_reused_format();
- if(choices=="" || choices.find('f') !=npos)
- test_format();
- if(choices.find('t') !=npos)
- test_try1();
- if(choices.find('y') !=npos)
- test_try2();
- if(choices.find('o') !=npos)
- test_opti_nullstream();
-#ifdef knelson
- if(choices=="" || choices.find('k') !=npos)
- test_format3();
-#endif
- return 0;
-}
-
-
-void test_snprintf()
+void test_sprintf()
{
using namespace std;
- // Check that snpintf is Unix98 compatible on the platform :
- char * buf = new char[4000];
+ vector<char> bufr;
+ bufr.reserve(4000);
+ char *buf = &bufr[0];
+
+ // Check that sprintf is Unix98 compatible on the platform :
sprintf(buf, fstring.c_str(), arg1, arg2, arg3);
if( strncmp( buf, res.c_str(), res.size()) != 0 ) {
cerr << endl << buf;
<< ", = " << t / tstream << " * nullStream \n";
}
-
-#ifdef knelson
-void test_format3()
-{
- using namespace std;
- boost::io::basic_oaltstringstream<char> oss;
- oss << KNelson::boost::format(fstring.c_str(), arg1, arg2, arg3);
- if(oss.str() != res ) {
- cerr << endl << oss.str();
- }
+} // benchmark
- boost::timer chrono;
- for(int i=0; i<NTests; ++i) {
- nullStream << KNelson::boost::format(fstring.c_str(), arg1, arg2, arg3);
- }
- double t = chrono.elapsed();
- cout << left << setw(20) <<"format3 time"<< right <<":" << setw(5) << t
- << ", = " << t / tpf << " * printf "
- << ", = " << t / tstream << " * nullStream \n" ;
+int main(int argc, char * argv[]) {
+ using namespace benchmark;
+ using namespace boost;
+ using namespace std;
+ const string::size_type npos = string::npos;
+
+ string choices = "";
+ if (1<argc) {
+ choices = (argv[1]); // profiling is easier launching only one.
+ NTests = 1000 * 1000; // andmoreprecise with many iterations
+ cout << "choices (" << choices << ") \n";
+ }
+
+ if (choices == "" || choices.find('p') != npos)
+ test_sprintf();
+ if (choices == "" || choices.find('n') != npos)
+ test_nullstream();
+ if (choices == "" || choices.find('1') != npos)
+ test_parsed_once_format();
+ if (choices == "" || choices.find('r') != npos)
+ test_reused_format();
+ if (choices == "" || choices.find('f') != npos)
+ test_format();
+ if (choices.find('t') != npos)
+ test_try1();
+ if (choices.find('y') != npos)
+ test_try2();
+ if (choices.find('o') != npos)
+ test_opti_nullstream();
+ return 0;
}
-
-#endif
+