473,756 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

output to a file (stream performance)

Lee
Hi,

I'm a stream virgin and am attempting to output strings to a file. My
approach is to write the string initially to a 'stringstream' and only when
complete write the stringstream to the file (ofstream).

The process works fine however appears to be rather slow. For example
outputting about 2Mb of data takes a couple of minutes (most of the time
appears to be writing to the stringstream) and as I'm creating several
hundred files in one run the whole time can run into hours. Oh, the files
are on my harddrive hence there is no network performance issues.

Is there anything basic I'm doing wrong (see code below) or does anyone have
any suggestions to improve this performance.

Thank in advance
Lee
stringstream m_outputFileRef erence;
ofstream m_outputFile ;

//initialisation for each file produced
m_outputFile.op en(fullPathName , fstream::out) ;
m_outputFileRef erence.str("") ;
m_outputFileRef erence.clear() ;

// output data using the format below
m_outputFileRef erence << CC_NUM_START_OP EN << tempStr << CC_NUM_START_CL OSE
;

when complete send the stringstream to the file
m_outputFile << m_outputFileRef erence.str()
m_outputFile.cl ose();

Mar 8 '06 #1
7 9993
Lee wrote:
Hi,

I'm a stream virgin and am attempting to output strings to a file. My
approach is to write the string initially to a 'stringstream' and only when
complete write the stringstream to the file (ofstream).

The process works fine however appears to be rather slow. For example
outputting about 2Mb of data takes a couple of minutes (most of the time
appears to be writing to the stringstream) and as I'm creating several
hundred files in one run the whole time can run into hours. Oh, the files
are on my harddrive hence there is no network performance issues.

Is there anything basic I'm doing wrong (see code below) or does anyone have
any suggestions to improve this performance.

Thank in advance
Lee
stringstream m_outputFileRef erence;
ofstream m_outputFile ;

//initialisation for each file produced
m_outputFile.op en(fullPathName , fstream::out) ;
m_outputFileRef erence.str("") ;
m_outputFileRef erence.clear() ;

// output data using the format below
m_outputFileRef erence << CC_NUM_START_OP EN << tempStr << CC_NUM_START_CL OSE
;

when complete send the stringstream to the file
m_outputFile << m_outputFileRef erence.str()
m_outputFile.cl ose();


why not just:

ofstream m_outputFile(fu llPathName, fstream::out);
m_outputFile << CC_NUM_START_OP EN << tempStr << CC_NUM_START_CL OSE;

Regards,
Ben
Mar 8 '06 #2
Lee wrote:
My
approach is to write the string initially to a 'stringstream' and only
when complete write the stringstream to the file (ofstream).
Why though? You could immediately write to the file.
The process works fine however appears to be rather slow.
It probably depends on the implementation of the stream buffer
underlying the string stream: some implementation stick to the
[original] words in the standard which mandate that the buffer
in a string stream shall increase by just one position and/or
they implement the buffer to grow by some fixed amount, e.g.
128 characters (yes, I have seen this is in actual code of a
commercial 'basic_stringbu f' implementation) . This will cause
the string stream to spent its time mostly for copying
increasingly larger chunks of memory around.

The obvious work-around is to avoid this by using a file stream
immediately and just streaming the data there. If this is not
an option for whatever reason, the best bet is to *not* use a
string stream but rather a stream based on a simple handcrafted
stream buffer which simply extends the internal buffer by some
factor e.g. duplicating the size whenever the buffer runs full.
I think I have posted the corresponding code in the past but it
is not really hard to create anyway (just something like 20 lines
of code).
stringstream m_outputFileRef erence;
There is no good reason to use a 'std::stringstr eam' in this
situation anyway: you want to use a 'std::ostringst ream'. However,
this change will probably not remove your problem.
ofstream m_outputFile ;

//initialisation for each file produced
m_outputFile.op en(fullPathName , fstream::out) ; m_outputFileRef erence.str("") ;
m_outputFileRef erence.clear() ;


You don't need to perform the above two operations: after
construction, the string stream is empty and in a 'good()'
state.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #3
Lee
I tried this originally but when the files were across a network the
performance was even worse.

Lee

"benben" <be******@yahoo .com.au> wrote in message
news:44******** **************@ news.optusnet.c om.au...
Lee wrote:
Hi,

I'm a stream virgin and am attempting to output strings to a file. My
approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file (ofstream).

The process works fine however appears to be rather slow. For example
outputting about 2Mb of data takes a couple of minutes (most of the time
appears to be writing to the stringstream) and as I'm creating several
hundred files in one run the whole time can run into hours. Oh, the files are on my harddrive hence there is no network performance issues.

Is there anything basic I'm doing wrong (see code below) or does anyone have any suggestions to improve this performance.

Thank in advance
Lee
stringstream m_outputFileRef erence;
ofstream m_outputFile ;

//initialisation for each file produced
m_outputFile.op en(fullPathName , fstream::out) ;
m_outputFileRef erence.str("") ;
m_outputFileRef erence.clear() ;

// output data using the format below
m_outputFileRef erence << CC_NUM_START_OP EN << tempStr << CC_NUM_START_CL OSE ;

when complete send the stringstream to the file
m_outputFile << m_outputFileRef erence.str()
m_outputFile.cl ose();


why not just:

ofstream m_outputFile(fu llPathName, fstream::out);
m_outputFile << CC_NUM_START_OP EN << tempStr << CC_NUM_START_CL OSE;

Regards,
Ben

Mar 8 '06 #4
> I tried this originally but when the files were across a network the
performance was even worse.


Looks like the ofstream buffer isn't generous enough...and so it causes
lots of network traffic if I am not too mistaken.

If cross-platform is not an issue you may try platform-dependent support
which tends to have more optimization features.

Using a stringstream like a buffer is very odd a solution.

Regards,
Ben
Mar 8 '06 #5
Lee
Many thanks for the reply. I tried using the file stream directly but when
the files were over a network the performance was even worse.

Using a hand crafted stream buffer sounds good. I'm not to sure how
exactly - can this be achieved by creating a new class based upon streambuf
and utilsing the 'setbuf' function to control the buffer size.

thanks
Lee
"Dietmar Kuehl" <di***********@ yahoo.com> wrote in message
news:47******** ****@individual .net...
Lee wrote:
My
approach is to write the string initially to a 'stringstream' and only
when complete write the stringstream to the file (ofstream).


Why though? You could immediately write to the file.
The process works fine however appears to be rather slow.


It probably depends on the implementation of the stream buffer
underlying the string stream: some implementation stick to the
[original] words in the standard which mandate that the buffer
in a string stream shall increase by just one position and/or
they implement the buffer to grow by some fixed amount, e.g.
128 characters (yes, I have seen this is in actual code of a
commercial 'basic_stringbu f' implementation) . This will cause
the string stream to spent its time mostly for copying
increasingly larger chunks of memory around.

The obvious work-around is to avoid this by using a file stream
immediately and just streaming the data there. If this is not
an option for whatever reason, the best bet is to *not* use a
string stream but rather a stream based on a simple handcrafted
stream buffer which simply extends the internal buffer by some
factor e.g. duplicating the size whenever the buffer runs full.
I think I have posted the corresponding code in the past but it
is not really hard to create anyway (just something like 20 lines
of code).
stringstream m_outputFileRef erence;


There is no good reason to use a 'std::stringstr eam' in this
situation anyway: you want to use a 'std::ostringst ream'. However,
this change will probably not remove your problem.
ofstream m_outputFile ;

//initialisation for each file produced
m_outputFile.op en(fullPathName , fstream::out) ;

m_outputFileRef erence.str("") ;
m_outputFileRef erence.clear() ;


You don't need to perform the above two operations: after
construction, the string stream is empty and in a 'good()'
state.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

Mar 8 '06 #6
Lee wrote:
Using a hand crafted stream buffer sounds good. I'm not to sure how
exactly - can this be achieved by creating a new class based upon
streambuf and utilsing the 'setbuf' function to control the buffer size.


'setbuf()' is the wrong tool: it has essentially no useful guarantees.
The only guarantee it has is that 'setbuf(0, 0)' will turn a file
stream to become unbuffered. You might be able to set a buffer size
suiting your need for file streams but this is implementation specific.

To create a useful surrogate for a string stream, you would derive a
class from 'std::streambuf ' and essentially just override the
'overflow()' method to install more room. Essentially the code for
the stream buffer would look like this (note: the code is untested,
not even compiled):

class mystringbuf:
std::streambuf
{
public:
enum { initial = 1024 };
mystringbuf(): m_buffer(new char[initial])
{ this->setp(this->m_buffer, this->m_buffer + initial); }
~mystringbuf() { delete[] this->m_buffer; }

private:
int_type overflow(int_ty pe c)
{
// increase the buffer
if (this->pptr() == this->epptr())
{
ptrdiff_t size = this->pptr() - this->pbase();
char* tmp = new char[2 * size];
std::copy(this->pbase(), this->pptr(), tmp);
this->setp(tmp, tmp + 2 * size);
this->pbump(size);
std::swap(tmp, this->m_buffer);
delete[] tmp;
}

// put the character into the buffer
if (c != std::char_trait s<char>::eof() )
{
*this->pptr() = std::char_trait s<char>::to_cha r_type(c);
this->pbump(1);
}

// signal success
return std::char_trait s<char>::not_eo f(c);
}
char* m_buffer;
};

You would, of course, still need some method to access the buffer
but this can be anything to your liking. I would probably provide
a pair of iterators and/or a pointer to the start plus the current
size (this would be useful to pass it to 'sputn()' of the file
stream's 'rdbuf()').
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #7

Lee wrote:
Hi,

I'm a stream virgin and am attempting to output strings to a file. My
approach is to write the string initially to a 'stringstream' and only when
complete write the stringstream to the file (ofstream). [snip] Is there anything basic I'm doing wrong (see code below) or does anyone have
any suggestions to improve this performance.

[snip]

http://groups.google.com/group/perfo...73f4d1a05cfbd1 contains
various testsuites to measure comparative performance of "Reading file
into string".
Perhaps, it is worth building similar testsuites to measure comparative
performance of "Writing string to file", for instance.

---
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 8 '06 #8

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

Similar topics

1
1507
by: joy_julia446 | last post by:
Hi there, I would like to print my result as below in an output file. I would appreciate if someone can give me some advice. RESULT ClassA,96.678,88.196,8.048,-0.233,456.231,5.890, 1 11.371,-0.906,-0.025,9999.888,76.888,0.001,3456.899 2 20.422,-20.979,0.422,4567.987,987.987,0.985,0.445, 3 -0.044,0.905,1.335,890.987,34.567,32.235,76.874; 4
4
2209
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some additinional methods to deal with it, i.e. open/close/write: classA { private: const std::auto_ptr<std::ofstream> filePtr;
3
2223
by: frustrated | last post by:
I am trying to share a file stream between two threads, but havent got a clue as to how to do it. The first thread will be reading the file, and the second thread will(/might) be writing to the same file stream. I was going to pass a ref to the file stream to the second thread, but the problem comes when I am trying to read from one section of the stream and write to another. I dont want one thread changing the position of the stream...
10
3167
by: m00nm0nkey | last post by:
Hello. I am trying to split a file with 334,386 lines into seperate files of 50,000 each. This is the code i am running: Dim intFragmentRawIndex As Integer Dim swRawDataFile As StreamWriter Dim intNewRawIndex As Integer Dim strNewRawDataFileName As String Dim intFragmentCallCount As Integer = 0
0
1810
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner; public class project { public static void main( String args ) {
1
1271
by: rahulrsh | last post by:
I am doing an image processing project which requires multiple threads to operate and write to an output file stream... Is is possible by some function to read and write specific locations of a file stream or is everything serial ?
5
6868
by: Nitin Mahajan | last post by:
Guys Is there a way in C# to create a word object directly from a memory stream without passing that to hard disk (file stream). I think it doesn't makes sense to create a file just to read it again in to word object. If some one has some brilliant idea please share. In a nutshell this is what I'm currently doing ByteArray --File Stream --Word Object and this I want to do
5
1986
by: cnoe | last post by:
I need to program this. I'm not asking any code, but an idea from you would be helpful. Thanks. I open a binary file stream, which has millions or trillions of 1's & 0's some thing like this 1100100011101110111.......... for easy understanding lets take this
0
1203
code green
by: code green | last post by:
I am using the following to force a file dialog box to open header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Content-type: text/csv');Then open a file stream with return $this->handle = fopen('php://output','w');Which directs a file strean to output rather than write to a file. Well that is how I understand it. I then write the data then close the stream with fclose($this->handle); Problem is html output which I...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
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
8723
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
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.