473,804 Members | 3,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The performance of strstream vs. stringstrem

I wrote a function like this:

#define LEN 8
void foo(ostream& decry_stream )
{
ifstream ifile( filepath.c_str( ), ios_base::binar y );
while ( !ifile.eof() )
{
ifile.read((cha r *)(&aucCipherTe xt), LEN);
read_count = ifile.gcount();

//do some work of decryption...
Decrypt(aucCiph erText, aucDecryText, LEN);

for (i = 0; i < read_count; i++)
decry_stream << aucDecryText[i];
}

return;
}

The input file is about 1M bytes. The time cosumed when i used a
ofstream, strstream and stringstream as the parameter respectively is:
---------------------
ofstream ofile;
foo(ofile);
---------------------
ostrstream osst;
foo(osst);
---------------------
ostringstream ostringst;
foo(ostringst);
---------------------

the time consumed:
-------------
less than 1s;
-------------
less than 1s;
-------------
more than 4s;
--------------

I don't know why stringstream is much slower than the other two. Any
help is appreciated!

Jul 23 '05 #1
7 3820
oh, the size of input file for testing is about 270K bytes, not 1M
bytes. osrry.

Jul 23 '05 #2

"forrest" <kk*******@gmai l.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I wrote a function like this:

#define LEN 8
void foo(ostream& decry_stream )
{
ifstream ifile( filepath.c_str( ), ios_base::binar y );
while ( !ifile.eof() )
{
ifile.read((cha r *)(&aucCipherTe xt), LEN);
read_count = ifile.gcount();

//do some work of decryption...
Decrypt(aucCiph erText, aucDecryText, LEN);

for (i = 0; i < read_count; i++)
decry_stream << aucDecryText[i];
}

return;
}

The input file is about 1M bytes. The time cosumed when i used a
ofstream, strstream and stringstream as the parameter respectively is:
---------------------
ofstream ofile;
foo(ofile);
---------------------
ostrstream osst;
foo(osst);
---------------------
ostringstream ostringst;
foo(ostringst);
---------------------

the time consumed:
-------------
less than 1s;
-------------
less than 1s;
-------------
more than 4s;
--------------

I don't know why stringstream is much slower than the other two. Any
help is appreciated!


It's impossible for us to tell, since you don't tell us the
states of the output stream objects that get passed to your
function. But I suspect the difference you're seeing is due
to differences in allocation strategies between your implementation' s
'ostringstream' and 'ostrstream' types. E.g. I ran a test using
(VC++) default behavior will results similar to yours, but then
preallocated the storage (a static array) for 'ostringstream' , and
used the default behavior of 'ostrstream'. The 'ostringstream' ran
approximately 2.5 times as fast as 'ostrstream'. I.e.:

char data[1024 * 1024];

int main()
{
std::ostrstream ost;
std::ostringstr eam oss(std::string (data, data + sizeof data));
foo(ost);
foo(oss);
return 0;
}
ostrstream: 11329 ms
ostringstream: 4375 ms
-Mike
Jul 23 '05 #3

"forrest" <kk*******@gmai l.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
oh, the size of input file for testing is about 270K bytes, not 1M
bytes. osrry.


The test file I used was approx 1.3 MB
(Pentium 2.8 Ghz, 512 MB RAM)

-Mike

Jul 23 '05 #4
>..., but then
preallocated the storage (a static array) for 'ostringstream'


That does no such thing. There is no "pre-allocation" in ostringstream.
It just sets the internal buffer to what is passed as std::string. That is
all.
And it is not clear from the standard as to where the put seek position is
after such an operation (I cannot remember whether a DR resolved this or
not).
Will any << overwrite the internal buffer?
Will and << append to the internal buffer?

Stephen Howe
Jul 23 '05 #5
You can get better performance for the fstreams if read/writing sequentially
by using a bigger buffer:

ifile.rdbuf()->pubsetbuf(NULL , 16384);

after a successful open and before first read/write.

Stephen Howe
Jul 23 '05 #6

"Stephen Howe" <no***@nowhere. com> wrote in message
news:42******** *************@p tn-nntp-reader04.plus.n et...
..., but then
preallocated the storage (a static array) for 'ostringstream'
That does no such thing. There is no "pre-allocation" in ostringstream.
It just sets the internal buffer to what is passed as std::string. That is
all.


Perhaps it's not technically called 'pre-allocation', but
it does serve the purpose, in that there's ready-to-use
storage when the stringstream is created.
And it is not clear from the standard as to where the put seek position is
after such an operation (I cannot remember whether a DR resolved this or
not).
Will any << overwrite the internal buffer?
Will and << append to the internal buffer?


It's certainly not definitive, but the result I got
indicates that my implementation overwrites (I presume
only until the buffer is full, whereupon I'd expect
allocation to occur).

-Mike
Jul 23 '05 #7
> Perhaps it's not technically called 'pre-allocation', but
it does serve the purpose, in that there's ready-to-use
storage when the stringstream is created.


No it is not the same. It is entirely analogous to

std::vector <int> v;

v.reserve(4000) ; // 1
v.resize(4000); // 2

// 1 reserves memory but v.size() is still 0. There are no elements in the
vector unlike //2. In contrast

char data[1024 * 1024];
std::ostringstr eam oss(std::string (data, data + sizeof data));
std::string s = oss.str();

The fact that s.size() is not 0, means that there _IS_ something occupying
the ostringstream (every character all 0). It is not a reserve of memory.

I wish stringstream's interface was a little better. If I get a chance of
raising it here in Oxford, UK at ACCU with some of the ISO C++ committee
members here, I will do so.

Stephen Howe
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2255
by: Bpb | last post by:
I'm trying to use a string stored in a strstream object as the filename parameter in the open call on an fstream object.. It requires a const char* but no matter what I try I can't seem to get a raw pointer to the string stored in the strstream object.. Anyone have an idea I'm overlooking? Thanks in advance.. -- http://www.SPAMdelusionalmindSUCKS.com
4
1998
by: vishnu mahendra | last post by:
Can anyone please tell me what is the use of "strstream.h" header file and the functions in "strstream.h". where can i find documentation for that header file. thankyou in advance, vishnu.
6
2867
by: vishnu mahendra | last post by:
hello to all, #include<iostream.h> #include<conio.h> #include <strstream.h> void main() { clrscr(); strstream str; char *a = new char; int i;
2
2319
by: derek | last post by:
Hi, I'm trying to compile someone's code which uses ostrstream. I know it's declared in file strstream, but I can't find this file in the /usr/include/c++/3.3.3 directory. There is a file called sstream, but it doesn't work when I include sstream. Do you guys know what's wrong? Thanks in advance.
2
3871
by: b83503104 | last post by:
Hi, An old code is using stuff like #include <strstream.h> ostrstream *m_actionStream; and the compiler complained syntax error before `*' token I followed some previous posts and tried things like #include <strstream.h> or
10
14889
by: Aaron Gray | last post by:
Hi, I have an application that uses 'strstream' in just under half its .cc files, about 10 files. What is the replacement for 'strstream' ? What are my options for replacing it ? Many thanks in advance,
8
14197
by: berkay | last post by:
hi all,i need this header file can anyone send it.thanks all.
2
2702
by: Praveen Chandra | last post by:
Hi, I am trying to build a static library, while building the library i am getting the following error: fatal error C1189: #error : "No sstream/strstream implementation" Following is the code that is giving error: #ifdef HAVE_CONFIG_H
4
7297
by: morten44 | last post by:
here the output and my Compiler version, It is a basic example from Bjarne Stroustrups homepage, morten@Westparkstr42:~/Bjarne_Stroustrup/06_Chapter_06$ g++ dc_except.c dc_except.c:197:23: error: strstream.h: No such file or directory g++ -v Using built-in specs.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.