delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/11/25/14:49:21

From: Alex Vinokur <alexvn AT bigfoot DOT com>
Newsgroups: comp.os.msdos.djgpp,comp.lang.c++
Subject: ostringstream vs. ostrstream performance
Date: Sun, 25 Nov 2001 16:31:10 +0200
Organization: Scopus Network Technologies
Lines: 150
Message-ID: <3C0100AC.93129C4B@bigfoot.com>
NNTP-Posting-Host: 62.90.123.5
Mime-Version: 1.0
X-Trace: fu-berlin.de 1006698935 4532421 62.90.123.5 (16 [79865])
X-Mailer: Mozilla 4.7 [en] (Win98; I)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

===============================================================
Windows98
gpp : GNU C++ version 2.95.3 20010315/djgpp (release) (djgpp)
      compiled by GNU C version 2.95.3 20010315/djgpp (release).
===============================================================


Hi,

Here are some results of comparing performance :  'ostrstream vs.
ostringstream'.
It seems that the results are weird :
 ostringstream::operator<< is improbably slower than
ostrstream::operator<<.

Any explanation?
Something wrong in this experiment?

  Thanks in advance.

//------------- C++ Code : BEGIN -------------
#include <iostream>
#include <string>
#include <sstream>
#include <strstream>

#include <sys/time.h>
#include <utime.h>
#include <iomanip.h>
#include <assert.h>

#define TOTAL_ITERATIONS 1000
#define TOTAL_PER_CALLS  10
#define TEST_STRING_SIZE 125

#define SHOW_PERFORMANCE(x,y)  \
   cout.setf (ios::left, ios::adjustfield); \
 cout << setw (20)  \
      << string(x).c_str() \
      << " : "   \
      << setw (20)  \
      << string(y).c_str() \
      << " -> "   \
      << (end_time - start_time)/(TOTAL_ITERATIONS/TOTAL_PER_CALLS) \
      << endl


int main ()
{
uclock_t start_time;
uclock_t end_time;
string s1 (TEST_STRING_SIZE, 'S');

ostrstream var1_ostrstream;
ostringstream var1_ostringstream;

  //---------------------------------
  start_time = uclock();
  for (unsigned long i = 0; i < TOTAL_ITERATIONS; i++)
  {
    ostrstream var2_ostrstream;
    var2_ostrstream << s1;
    var2_ostrstream.rdbuf()->freeze(0);
  }
  end_time = uclock();
  SHOW_PERFORMANCE("ostrstream", "ctor, out, dtor");

  start_time = uclock();
  for (unsigned long i = 0; i < TOTAL_ITERATIONS; i++)
  {
    ostringstream var2_ostringstream;
    var2_ostringstream << s1;
  }
  end_time = uclock();
  SHOW_PERFORMANCE("ostringstream", "ctor, out, dtor");
  cout << endl;

  //---------------------------------
  start_time = uclock();
  for (unsigned long i = 0; i < TOTAL_ITERATIONS; i++)
  {
    ostrstream var2_ostrstream;
    var2_ostrstream.rdbuf()->freeze(0);
  }
  end_time = uclock();
  SHOW_PERFORMANCE("ostrstream", "ctor, dtor");

  start_time = uclock();
  for (unsigned long i = 0; i < TOTAL_ITERATIONS; i++)
  {
    ostringstream var2_ostringstream;
  }
  end_time = uclock();
  SHOW_PERFORMANCE("ostringstream", "ctor, dtor");
  cout << endl;


  //---------------------------------
  start_time = uclock();
  for (unsigned long i = 0; i < TOTAL_ITERATIONS; i++)
  {
    var1_ostrstream << s1;
  }
  end_time = uclock();
  SHOW_PERFORMANCE("ostrstream", "out");

  start_time = uclock();
  for (unsigned long i = 0; i < TOTAL_ITERATIONS; i++)
  {
    var1_ostringstream << s1;
  }
  end_time = uclock();
  SHOW_PERFORMANCE("ostringstream", "out");
  cout << endl;

  var1_ostrstream.rdbuf()->freeze(0);

  return 0;
}

//-------------- C++ Code : END --------------


//-------------- Running : BEGIN -------------

%a.exe

ostrstream           : ctor, out, dtor      -> 50
ostringstream        : ctor, out, dtor      -> 1813

ostrstream           : ctor, dtor           -> 10
ostringstream        : ctor, dtor           -> 20

ostrstream           : out                  -> 23
ostringstream        : out                  -> 691032

//--------------- Running : END --------------




  ===========================
  Alex Vinokur
    mailto:alexvn AT bigfoot DOT com
    mailto:alexvn AT dr DOT com
    http://up.to/alexvn
    http://go.to/alexv_math
  ===========================


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019