File: bench_format.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (388 lines) | stat: -rw-r--r-- 10,976 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// -*- C++ -*-
//  Boost general library 'format'  ---------------------------
//  See http://www.boost.org for updates, documentation, and revision history.

//  Copyright (c) 2001 Samuel Krempp
//                  krempp@crans.ens-cachan.fr
//  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)

// 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 <iostream>
#include <iomanip>
#include <cstdio>  // sprintf
#include <cstring>
#include <fstream>
#include <cmath>   // floor
#include <boost/timer.hpp>

#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<char, std::char_traits<char> > 
{
public:
  NulStream();
  virtual ~NulStream();
  NulStreambuf*    rdbuf() {
    return static_cast< NulStreambuf* >(
                   ((std::basic_ostream<char, std::char_traits<char> > *) this) -> rdbuf() ) ;
  }
} ;
 

//-------------------------------------------------------------------------------------
//   NulStream implementation

NulStream::NulStream()  : std::basic_ostream<char, std::char_traits<char> > (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<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()
{
    using namespace std;

    // Check that snpintf is Unix98 compatible on the platform :
    char * buf = new char[4000];
    sprintf(buf, fstring.c_str(), arg1, arg2, arg3);
    if( strncmp( buf, res.c_str(), res.size()) != 0 ) {
      cerr << endl << buf;
    }
    // time the loop :
    boost::timer chrono;
    for(int i=0; i<NTests; ++i) {
      sprintf(buf, fstring.c_str(), arg1, arg2, arg3);
    }
    tpf=chrono.elapsed();
    cout  << left << setw(20) <<"printf time"<< right <<":" << tpf  << endl;
}

void test_try1()
{
  using namespace std;
  boost::io::basic_oaltstringstream<char> oss;
  oss << boost::format(fstring) % arg1 % arg2 % arg3;
  boost::timer chrono;
  int dummy=0;
  for(int i=0; i<NTests; ++i) {
      dummy += oss.cur_size();
  }
  double t = chrono.elapsed();
  cout  << left << setw(20) <<"try1 time"<< right <<":" << setw(5) << t
        << ",  = " << t / tpf << " * printf "
        << ",  = " << t / tstream << " * nullStream \n";
}

void test_try2()
{
  using namespace std;
  boost::io::basic_oaltstringstream<char> oss;
  oss << boost::format(fstring) % arg1 % arg2 % arg3;
  oss << "blas 34567890GGGGGGGGGGGGGGGGGGGGGGGGGGGGggggggggggggggggggggggggggg " << endl;
  string s = oss.cur_str();
  oss << s << s << s;
  oss.clear_buffer();
  oss << s << s;
  s = oss.cur_str();
  boost::timer chrono;
  int dummy=0;
  for(int i=0; i<NTests; ++i) {
      dummy += oss.cur_size();
  }
  double t = chrono.elapsed();
  cout  << left << setw(20) <<"try2 time"<< right <<":" << setw(5) << t
        << ",  = " << t / tpf << " * printf "
        << ",  = " << t / tstream << " * nullStream \n";
}

void do_stream(std::ostream& os) {
    using namespace std;
    std::ios_base::fmtflags f = os.flags();
    os << hex << showbase << internal << setfill('0') << setw(6) << arg3
       << dec << noshowbase << right << setfill(' ') 
       << " " 
       << scientific << setw(20) << setprecision(10) << uppercase << arg1 
       << setprecision(6) << nouppercase ;
    os.flags(f);
    os << " " << arg2 << " " 
       << showpos << setw(5) << internal << setfill('0') << arg3 << " \n" ;
    os.flags(f);
}

void test_nullstream()
{
    using namespace std;
    boost::timer chrono;
    boost::io::basic_oaltstringstream<char> oss;

    {   
        do_stream(oss);
        if(oss.str() != res ) {
            cerr << endl << oss.str() ;
        }
    }

    for(int i=0; i<NTests; ++i) { 
        do_stream(nullStream);
    }

//     for(int i=0; i<NTests; ++i) { 
//       std::ios_base::fmtflags f0 = nullStream.flags();
//       nullStream << hex << showbase << arg3
//                  << dec << noshowbase << " " 
//                  << scientific << setw(20) << setprecision(10) << uppercase <<  arg1 
//                  << setprecision(0);
//       nullStream.flags(f0);
//       nullStream << " " << arg2 << " " << arg3 << " \n" ;

//     }
    double t = chrono.elapsed();
    cout  << left << setw(20) <<"ostream time"<< right <<":" << setw(5) << t  
          << ",  = " << t / tpf << " * printf \n";
    tstream = t;
}

void test_opti_nullstream()
{
    using namespace std;
    boost::timer chrono;
    boost::io::basic_oaltstringstream<char> oss;
    //static const std::string fstring="%3$#x %1$20.10E %2$g %3$d \n";

    std::ios_base::fmtflags f0 = oss.flags(), f1, f2;
    streamsize p0 = oss.precision();
    {
      oss << hex << showbase; 
      f1 = oss.flags();
      oss << arg3;

      oss.flags(f0);
      oss << " " << scientific << setw(20) << setprecision(10) << uppercase;
      f2 = oss.flags();
      oss << arg1;

      oss.flags(f0); oss.precision(p0);
      oss << " " << arg2 << " " << arg3 << " \n" ;
    
      if(oss.str() != res ) {
        cerr << endl << oss.str() ;
      }
    }

    for(int i=0; i<NTests; ++i) { 
      nullStream.flags(f1);
      nullStream << arg3;

      nullStream << setw(20) << setprecision(10);
      nullStream.flags(f2);
      nullStream << arg1;

      nullStream.flags(f0); nullStream.precision(p0);
      nullStream << " " << arg2 << " " << arg3 << " \n" ;
    }
    double t = chrono.elapsed();
    cout  << left << setw(20) <<"opti-stream time"<< right <<":" << setw(5) << t  
          << ",  = " << t / tpf << " * printf \n";
    //    tstream = t;
}

void test_parsed_once_format()
{
    using namespace std;
    static const boost::format fmter(fstring);

    boost::io::basic_oaltstringstream<char> oss;
    oss << boost::format(fmter) % arg1 % arg2 % arg3 ;
    if( oss.str() != res ) {
      cerr << endl << oss.str();
    }

    // not only is the format-string parsed once,
    // but also the buffer of the internal stringstream is already allocated.

    boost::timer chrono;        
    for(int i=0; i<NTests; ++i) {
        nullStream << boost::format(fmter) % arg1 % arg2 % arg3;
    }
    double t=chrono.elapsed();
    cout  << left << setw(20) <<"parsed-once time"<< right <<":" << setw(5) << t 
          << ",  = " << t / tpf << " * printf "
          << ",  = " << t / tstream << " * nullStream \n";
}

void test_reused_format()
{
  using namespace std;
  boost::io::basic_oaltstringstream<char> oss;
  oss << boost::format(fstring) % arg1 % arg2 % arg3;
  if(oss.str() != res ) {
    cerr << endl << oss.str();
  }

  boost::timer chrono;
  boost::format fmter;
  for(int i=0; i<NTests; ++i) {
    nullStream << fmter.parse(fstring) % arg1 % arg2 % arg3;
  }
  double t = chrono.elapsed();
  cout  << left << setw(20) <<"reused format time"<< right <<":" << setw(5) << t
        << ",  = " << t / tpf << " * printf "
        << ",  = " << t / tstream << " * nullStream \n";
}

void test_format()
{
  using namespace std;
  boost::io::basic_oaltstringstream<char> oss;
  oss << boost::format(fstring) % arg1 % arg2 % arg3;
  if(oss.str() != res ) {
    cerr << endl << oss.str();
  }

  boost::timer chrono;
  for(int i=0; i<NTests; ++i) {
    nullStream << boost::format(fstring) % arg1 % arg2 % arg3;
  }
  double t = chrono.elapsed();
  cout  << left << setw(20) <<"format time"<< right <<":" << setw(5) << t
        << ",  = " << t / tpf << " * printf "
        << ",  = " << 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();
  }

  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" ;
}
 
#endif