Click to See Complete Forum and Search --> : performance of fstream in C++


Quakey
April 17th, 1999, 02:29 PM
Hi there,
Could anyone tell me which is faster to save data with fstream---
Situation: A line of text containing many segment are to be stored to a file, for example: Bob Is Cool.
Methods:
(1) Use fstream and output one word a time. Like
out << "Bob ";
out << "Is ";
out << "Cool.";
(2) Use fstream and output the whole sentence at one. Like
out << "Bob Is Cool.";
Which method is faster? Thanks for help.

Roger Osborn
April 17th, 1999, 04:03 PM
2 is likely to be faster, but there probably isn't as much difference in it as you might expect. Method 1 is unlikely to cause a disk seek on each line because in both 1 and 2 it's all memory cached until something causes the file to be flushed. So the difference is memory allocation and function call overhead, not disk hardware times.