473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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::binary );
while ( !ifile.eof() )
{
ifile.read((char *)(&aucCipherText), LEN);
read_count = ifile.gcount();

//do some work of decryption...
Decrypt(aucCipherText, 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 3794
oh, the size of input file for testing is about 270K bytes, not 1M
bytes. osrry.

Jul 23 '05 #2

"forrest" <kk*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I wrote a function like this:

#define LEN 8
void foo(ostream& decry_stream )
{
ifstream ifile( filepath.c_str(), ios_base::binary );
while ( !ifile.eof() )
{
ifile.read((char *)(&aucCipherText), LEN);
read_count = ifile.gcount();

//do some work of decryption...
Decrypt(aucCipherText, 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::ostringstream 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*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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*********************@ptn-nntp-reader04.plus.net...
..., 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::ostringstream 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
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...
4
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
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
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...
2
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...
10
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...
8
by: berkay | last post by:
hi all,i need this header file can anyone send it.thanks all.
2
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.