]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/format/benchmark/bench_format.cpp
2 // Boost general library 'format' ---------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
5 // Copyright (c) 2001 Samuel Krempp
6 // krempp@crans.ens-cachan.fr
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 // several suggestions from Jens Maurer
13 // ------------------------------------------------------------------------------
14 // bench_variants.cc : do the same task, with snprintf, stream, and format
15 // and compare their times.
17 // This benchmark is provided purely for information.
18 // It might not even compile as-is,
19 // or not give any sensible results.
20 // (e.g., it expects sprintf to be POSIX compliant)
22 // ------------------------------------------------------------------------------
27 #include <cstdio> // sprintf
30 #include <cmath> // floor
31 #include <boost/timer.hpp>
33 #include <boost/format.hpp>
39 #include "boost/format3.hpp"
46 // portable /dev/null stream equivalent, by James Kanze, http://www.gabi-soft.de
47 class NulStreambuf
: public std::streambuf
51 setp( dummyBuffer
, dummyBuffer
+ 64 ) ;
53 virtual int overflow( int c
);
54 virtual int underflow();
56 char dummyBuffer
[ 64 ] ;
59 class NulStream
: public std::basic_ostream
<char, std::char_traits
<char> >
64 NulStreambuf
* rdbuf() {
65 return static_cast< NulStreambuf
* >(
66 ((std::basic_ostream
<char, std::char_traits
<char> > *) this) -> rdbuf() ) ;
71 //-------------------------------------------------------------------------------------
72 // NulStream implementation
74 NulStream::NulStream() : std::basic_ostream
<char, std::char_traits
<char> > (NULL
) {
75 init( new NulStreambuf
) ;
78 NulStream::~NulStream() {
82 int NulStreambuf::underflow(){ return std::ios::traits_type::eof();
85 int NulStreambuf::overflow( int c
){
86 setp( dummyBuffer
, dummyBuffer
+ 64 ) ;
87 return (c
== std::ios::traits_type::eof()) ? '\0' : c
;
92 // -------------------------------------------------------------------------------------
94 static int NTests
= 300000;
96 //static std::stringstream nullStream;
97 static NulStream nullStream
;
98 static double tstream
, tpf
;
99 //static const std::string fstring="%3$#x %1$20.10E %2$g %3$d \n";
100 static const std::string fstring
="%3$0#6x %1$20.10E %2$g %3$0+5d \n";
101 static const double arg1
=45.23;
102 static const double arg2
=12.34;
103 static const int arg3
=23;
104 static const std::string res
=
105 "0x0017 4.5230000000E+01 12.34 +0023 \n";
106 //static const std::string res = "23.0000 4.5230000000E+01 12.34 23 \n";
107 void test_snprintf();
108 void test_nullstream();
109 void test_opti_nullstream();
110 void test_parsed_once_format();
111 void test_reused_format();
120 int main(int argc
, char * argv
[]) {
121 using namespace boost
;
123 const string::size_type npos
= string::npos
;
127 choices
= (argv
[1]); // profiling is easier launching only one.
128 NTests
= 1000*1000; // andmoreprecise with many iterations
129 cout
<< "choices (" << choices
<< ") \n";
132 if(choices
=="" || choices
.find('p') !=npos
)
134 if(choices
=="" || choices
.find('n') !=npos
)
136 if(choices
=="" || choices
.find('1') !=npos
)
137 test_parsed_once_format();
138 if(choices
=="" || choices
.find('r') !=npos
)
139 test_reused_format();
140 if(choices
=="" || choices
.find('f') !=npos
)
142 if(choices
.find('t') !=npos
)
144 if(choices
.find('y') !=npos
)
146 if(choices
.find('o') !=npos
)
147 test_opti_nullstream();
149 if(choices
=="" || choices
.find('k') !=npos
)
160 // Check that snpintf is Unix98 compatible on the platform :
161 char * buf
= new char[4000];
162 sprintf(buf
, fstring
.c_str(), arg1
, arg2
, arg3
);
163 if( strncmp( buf
, res
.c_str(), res
.size()) != 0 ) {
168 for(int i
=0; i
<NTests
; ++i
) {
169 sprintf(buf
, fstring
.c_str(), arg1
, arg2
, arg3
);
171 tpf
=chrono
.elapsed();
172 cout
<< left
<< setw(20) <<"printf time"<< right
<<":" << tpf
<< endl
;
178 boost::io::basic_oaltstringstream
<char> oss
;
179 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
182 for(int i
=0; i
<NTests
; ++i
) {
183 dummy
+= oss
.cur_size();
185 double t
= chrono
.elapsed();
186 cout
<< left
<< setw(20) <<"try1 time"<< right
<<":" << setw(5) << t
187 << ", = " << t
/ tpf
<< " * printf "
188 << ", = " << t
/ tstream
<< " * nullStream \n";
194 boost::io::basic_oaltstringstream
<char> oss
;
195 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
196 oss
<< "blas 34567890GGGGGGGGGGGGGGGGGGGGGGGGGGGGggggggggggggggggggggggggggg " << endl
;
197 string s
= oss
.cur_str();
204 for(int i
=0; i
<NTests
; ++i
) {
205 dummy
+= oss
.cur_size();
207 double t
= chrono
.elapsed();
208 cout
<< left
<< setw(20) <<"try2 time"<< right
<<":" << setw(5) << t
209 << ", = " << t
/ tpf
<< " * printf "
210 << ", = " << t
/ tstream
<< " * nullStream \n";
213 void do_stream(std::ostream
& os
) {
215 std::ios_base::fmtflags f
= os
.flags();
216 os
<< hex
<< showbase
<< internal
<< setfill('0') << setw(6) << arg3
217 << dec
<< noshowbase
<< right
<< setfill(' ')
219 << scientific
<< setw(20) << setprecision(10) << uppercase
<< arg1
220 << setprecision(6) << nouppercase
;
222 os
<< " " << arg2
<< " "
223 << showpos
<< setw(5) << internal
<< setfill('0') << arg3
<< " \n" ;
227 void test_nullstream()
231 boost::io::basic_oaltstringstream
<char> oss
;
235 if(oss
.str() != res
) {
236 cerr
<< endl
<< oss
.str() ;
240 for(int i
=0; i
<NTests
; ++i
) {
241 do_stream(nullStream
);
244 // for(int i=0; i<NTests; ++i) {
245 // std::ios_base::fmtflags f0 = nullStream.flags();
246 // nullStream << hex << showbase << arg3
247 // << dec << noshowbase << " "
248 // << scientific << setw(20) << setprecision(10) << uppercase << arg1
249 // << setprecision(0);
250 // nullStream.flags(f0);
251 // nullStream << " " << arg2 << " " << arg3 << " \n" ;
254 double t
= chrono
.elapsed();
255 cout
<< left
<< setw(20) <<"ostream time"<< right
<<":" << setw(5) << t
256 << ", = " << t
/ tpf
<< " * printf \n";
260 void test_opti_nullstream()
264 boost::io::basic_oaltstringstream
<char> oss
;
265 //static const std::string fstring="%3$#x %1$20.10E %2$g %3$d \n";
267 std::ios_base::fmtflags f0
= oss
.flags(), f1
, f2
;
268 streamsize p0
= oss
.precision();
270 oss
<< hex
<< showbase
;
275 oss
<< " " << scientific
<< setw(20) << setprecision(10) << uppercase
;
279 oss
.flags(f0
); oss
.precision(p0
);
280 oss
<< " " << arg2
<< " " << arg3
<< " \n" ;
282 if(oss
.str() != res
) {
283 cerr
<< endl
<< oss
.str() ;
287 for(int i
=0; i
<NTests
; ++i
) {
288 nullStream
.flags(f1
);
291 nullStream
<< setw(20) << setprecision(10);
292 nullStream
.flags(f2
);
295 nullStream
.flags(f0
); nullStream
.precision(p0
);
296 nullStream
<< " " << arg2
<< " " << arg3
<< " \n" ;
298 double t
= chrono
.elapsed();
299 cout
<< left
<< setw(20) <<"opti-stream time"<< right
<<":" << setw(5) << t
300 << ", = " << t
/ tpf
<< " * printf \n";
304 void test_parsed_once_format()
307 static const boost::format
fmter(fstring
);
309 boost::io::basic_oaltstringstream
<char> oss
;
310 oss
<< boost::format(fmter
) % arg1
% arg2
% arg3
;
311 if( oss
.str() != res
) {
312 cerr
<< endl
<< oss
.str();
315 // not only is the format-string parsed once,
316 // but also the buffer of the internal stringstream is already allocated.
319 for(int i
=0; i
<NTests
; ++i
) {
320 nullStream
<< boost::format(fmter
) % arg1
% arg2
% arg3
;
322 double t
=chrono
.elapsed();
323 cout
<< left
<< setw(20) <<"parsed-once time"<< right
<<":" << setw(5) << t
324 << ", = " << t
/ tpf
<< " * printf "
325 << ", = " << t
/ tstream
<< " * nullStream \n";
328 void test_reused_format()
331 boost::io::basic_oaltstringstream
<char> oss
;
332 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
333 if(oss
.str() != res
) {
334 cerr
<< endl
<< oss
.str();
339 for(int i
=0; i
<NTests
; ++i
) {
340 nullStream
<< fmter
.parse(fstring
) % arg1
% arg2
% arg3
;
342 double t
= chrono
.elapsed();
343 cout
<< left
<< setw(20) <<"reused format time"<< right
<<":" << setw(5) << t
344 << ", = " << t
/ tpf
<< " * printf "
345 << ", = " << t
/ tstream
<< " * nullStream \n";
351 boost::io::basic_oaltstringstream
<char> oss
;
352 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
353 if(oss
.str() != res
) {
354 cerr
<< endl
<< oss
.str();
358 for(int i
=0; i
<NTests
; ++i
) {
359 nullStream
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
361 double t
= chrono
.elapsed();
362 cout
<< left
<< setw(20) <<"format time"<< right
<<":" << setw(5) << t
363 << ", = " << t
/ tpf
<< " * printf "
364 << ", = " << t
/ tstream
<< " * nullStream \n";
372 boost::io::basic_oaltstringstream
<char> oss
;
373 oss
<< KNelson::boost::format(fstring
.c_str(), arg1
, arg2
, arg3
);
374 if(oss
.str() != res
) {
375 cerr
<< endl
<< oss
.str();
379 for(int i
=0; i
<NTests
; ++i
) {
380 nullStream
<< KNelson::boost::format(fstring
.c_str(), arg1
, arg2
, arg3
);
382 double t
= chrono
.elapsed();
383 cout
<< left
<< setw(20) <<"format3 time"<< right
<<":" << setw(5) << t
384 << ", = " << t
/ tpf
<< " * printf "
385 << ", = " << t
/ tstream
<< " * nullStream \n" ;