]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/format/benchmark/bench_format.cpp
393df7583456b740fec06ae4c183464ba9000e1f
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 sprintf, 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>
34 #include <boost/format.hpp>
36 // portable /dev/null stream equivalent, by James Kanze, http://www.gabi-soft.de
37 class NulStreambuf
: public std::streambuf
41 setp( dummyBuffer
, dummyBuffer
+ 64 ) ;
43 virtual int overflow( int c
);
44 virtual int underflow();
46 char dummyBuffer
[ 64 ] ;
49 class NulStream
: public std::basic_ostream
<char, std::char_traits
<char> >
54 NulStreambuf
* rdbuf() {
55 return static_cast< NulStreambuf
* >(
56 ((std::basic_ostream
<char, std::char_traits
<char> > *) this) -> rdbuf() ) ;
61 //-------------------------------------------------------------------------------------
62 // NulStream implementation
64 NulStream::NulStream() : std::basic_ostream
<char, std::char_traits
<char> > (NULL
) {
65 init( new NulStreambuf
) ;
68 NulStream::~NulStream() {
72 int NulStreambuf::underflow(){ return std::ios::traits_type::eof();
75 int NulStreambuf::overflow( int c
){
76 setp( dummyBuffer
, dummyBuffer
+ 64 ) ;
77 return (c
== std::ios::traits_type::eof()) ? '\0' : c
;
82 // -------------------------------------------------------------------------------------
86 static int NTests
= 300000;
88 //static std::stringstream nullStream;
89 static NulStream nullStream
;
90 static double tstream
, tpf
;
91 //static const std::string fstring="%3$#x %1$20.10E %2$g %3$d \n";
92 static const std::string fstring
="%3$0#6x %1$20.10E %2$g %3$0+5d \n";
93 static const double arg1
=45.23;
94 static const double arg2
=12.34;
95 static const int arg3
=23;
96 static const std::string res
=
97 "0x0017 4.5230000000E+01 12.34 +0023 \n";
98 //static const std::string res = "23.0000 4.5230000000E+01 12.34 23 \n";
101 void test_nullstream();
102 void test_opti_nullstream();
103 void test_parsed_once_format();
104 void test_reused_format();
115 char *buf
= &bufr
[0];
117 // Check that sprintf is Unix98 compatible on the platform :
118 sprintf(buf
, fstring
.c_str(), arg1
, arg2
, arg3
);
119 if( strncmp( buf
, res
.c_str(), res
.size()) != 0 ) {
124 for(int i
=0; i
<NTests
; ++i
) {
125 sprintf(buf
, fstring
.c_str(), arg1
, arg2
, arg3
);
127 tpf
=chrono
.elapsed();
128 cout
<< left
<< setw(20) <<"printf time"<< right
<<":" << tpf
<< endl
;
134 boost::io::basic_oaltstringstream
<char> oss
;
135 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
138 for(int i
=0; i
<NTests
; ++i
) {
139 dummy
+= oss
.cur_size();
141 double t
= chrono
.elapsed();
142 cout
<< left
<< setw(20) <<"try1 time"<< right
<<":" << setw(5) << t
143 << ", = " << t
/ tpf
<< " * printf "
144 << ", = " << t
/ tstream
<< " * nullStream \n";
150 boost::io::basic_oaltstringstream
<char> oss
;
151 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
152 oss
<< "blas 34567890GGGGGGGGGGGGGGGGGGGGGGGGGGGGggggggggggggggggggggggggggg " << endl
;
153 string s
= oss
.cur_str();
160 for(int i
=0; i
<NTests
; ++i
) {
161 dummy
+= oss
.cur_size();
163 double t
= chrono
.elapsed();
164 cout
<< left
<< setw(20) <<"try2 time"<< right
<<":" << setw(5) << t
165 << ", = " << t
/ tpf
<< " * printf "
166 << ", = " << t
/ tstream
<< " * nullStream \n";
169 void do_stream(std::ostream
& os
) {
171 std::ios_base::fmtflags f
= os
.flags();
172 os
<< hex
<< showbase
<< internal
<< setfill('0') << setw(6) << arg3
173 << dec
<< noshowbase
<< right
<< setfill(' ')
175 << scientific
<< setw(20) << setprecision(10) << uppercase
<< arg1
176 << setprecision(6) << nouppercase
;
178 os
<< " " << arg2
<< " "
179 << showpos
<< setw(5) << internal
<< setfill('0') << arg3
<< " \n" ;
183 void test_nullstream()
187 boost::io::basic_oaltstringstream
<char> oss
;
191 if(oss
.str() != res
) {
192 cerr
<< endl
<< oss
.str() ;
196 for(int i
=0; i
<NTests
; ++i
) {
197 do_stream(nullStream
);
200 // for(int i=0; i<NTests; ++i) {
201 // std::ios_base::fmtflags f0 = nullStream.flags();
202 // nullStream << hex << showbase << arg3
203 // << dec << noshowbase << " "
204 // << scientific << setw(20) << setprecision(10) << uppercase << arg1
205 // << setprecision(0);
206 // nullStream.flags(f0);
207 // nullStream << " " << arg2 << " " << arg3 << " \n" ;
210 double t
= chrono
.elapsed();
211 cout
<< left
<< setw(20) <<"ostream time"<< right
<<":" << setw(5) << t
212 << ", = " << t
/ tpf
<< " * printf \n";
216 void test_opti_nullstream()
220 boost::io::basic_oaltstringstream
<char> oss
;
221 //static const std::string fstring="%3$#x %1$20.10E %2$g %3$d \n";
223 std::ios_base::fmtflags f0
= oss
.flags(), f1
, f2
;
224 streamsize p0
= oss
.precision();
226 oss
<< hex
<< showbase
;
231 oss
<< " " << scientific
<< setw(20) << setprecision(10) << uppercase
;
235 oss
.flags(f0
); oss
.precision(p0
);
236 oss
<< " " << arg2
<< " " << arg3
<< " \n" ;
238 if(oss
.str() != res
) {
239 cerr
<< endl
<< oss
.str() ;
243 for(int i
=0; i
<NTests
; ++i
) {
244 nullStream
.flags(f1
);
247 nullStream
<< setw(20) << setprecision(10);
248 nullStream
.flags(f2
);
251 nullStream
.flags(f0
); nullStream
.precision(p0
);
252 nullStream
<< " " << arg2
<< " " << arg3
<< " \n" ;
254 double t
= chrono
.elapsed();
255 cout
<< left
<< setw(20) <<"opti-stream time"<< right
<<":" << setw(5) << t
256 << ", = " << t
/ tpf
<< " * printf \n";
260 void test_parsed_once_format()
263 static const boost::format
fmter(fstring
);
265 boost::io::basic_oaltstringstream
<char> oss
;
266 oss
<< boost::format(fmter
) % arg1
% arg2
% arg3
;
267 if( oss
.str() != res
) {
268 cerr
<< endl
<< oss
.str();
271 // not only is the format-string parsed once,
272 // but also the buffer of the internal stringstream is already allocated.
275 for(int i
=0; i
<NTests
; ++i
) {
276 nullStream
<< boost::format(fmter
) % arg1
% arg2
% arg3
;
278 double t
=chrono
.elapsed();
279 cout
<< left
<< setw(20) <<"parsed-once time"<< right
<<":" << setw(5) << t
280 << ", = " << t
/ tpf
<< " * printf "
281 << ", = " << t
/ tstream
<< " * nullStream \n";
284 void test_reused_format()
287 boost::io::basic_oaltstringstream
<char> oss
;
288 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
289 if(oss
.str() != res
) {
290 cerr
<< endl
<< oss
.str();
295 for(int i
=0; i
<NTests
; ++i
) {
296 nullStream
<< fmter
.parse(fstring
) % arg1
% arg2
% arg3
;
298 double t
= chrono
.elapsed();
299 cout
<< left
<< setw(20) <<"reused format time"<< right
<<":" << setw(5) << t
300 << ", = " << t
/ tpf
<< " * printf "
301 << ", = " << t
/ tstream
<< " * nullStream \n";
307 boost::io::basic_oaltstringstream
<char> oss
;
308 oss
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
309 if(oss
.str() != res
) {
310 cerr
<< endl
<< oss
.str();
314 for(int i
=0; i
<NTests
; ++i
) {
315 nullStream
<< boost::format(fstring
) % arg1
% arg2
% arg3
;
317 double t
= chrono
.elapsed();
318 cout
<< left
<< setw(20) <<"format time"<< right
<<":" << setw(5) << t
319 << ", = " << t
/ tpf
<< " * printf "
320 << ", = " << t
/ tstream
<< " * nullStream \n";
325 int main(int argc
, char * argv
[]) {
326 using namespace benchmark
;
327 using namespace boost
;
329 const string::size_type npos
= string::npos
;
333 choices
= (argv
[1]); // profiling is easier launching only one.
334 NTests
= 1000 * 1000; // andmoreprecise with many iterations
335 cout
<< "choices (" << choices
<< ") \n";
338 if (choices
== "" || choices
.find('p') != npos
)
340 if (choices
== "" || choices
.find('n') != npos
)
342 if (choices
== "" || choices
.find('1') != npos
)
343 test_parsed_once_format();
344 if (choices
== "" || choices
.find('r') != npos
)
345 test_reused_format();
346 if (choices
== "" || choices
.find('f') != npos
)
348 if (choices
.find('t') != npos
)
350 if (choices
.find('y') != npos
)
352 if (choices
.find('o') != npos
)
353 test_opti_nullstream();