954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

stringstreams

Hi guys.Does anyone who studied the implementation of stringstreams know if :

char c;
   std::stringstream ss;

   ss << std::fstream ("file.xml",std::ios::in).rdbuf();//this is eqaul in terms of speed
  
   while (ss.get(c))  {} //with this?

   std::ifstream f("file.xml");
   while (f.get(c)) {} //or this?

Thanks a lot.

caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 

I don't understand the question. Are you asking if reading from a stringstream is faster than reading from an fstream?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I think i have several questions and a problem in how to put them.
1) Is the extraction operator using the get() function in case of fstream?
2) Does moving the file pointer imply another call to seekg()?
3) Would this mean stringstream's get() function would be faster?
4) Would this 'ss << std::fstream ("file.xml",std::ios::in).rdbuf();' be faster than both operations since the buffer is already filled?

I hope i'm not making a fool of myself :)

caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 
1) Is the extraction operator using the get() function in case of fstream?


Probably not. More likely is that it uses the member functions of the stream buffer directly (just like get()).2) Does moving the file pointer imply another call to seekg()?
No, the file pointer is at a much lower level than seekg().3) Would this mean stringstream's get() function would be faster?
It's fairly safe to assume that stringstream's get() function is faster, but that's because you're accessing strings in memory rather than hardware.4) Would this 'ss << std::fstream ("file.xml",std::ios::in).rdbuf();' be faster than both operations since the buffer is already filled?
Well, obviously it comes down to the quality of the implementation, but writing the stream buffer is typically the fastest way to dump the contents of a stream.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Reading from a hard drive is typically around a million times slower than reading/writing to memory. I read that somewhere, don't quote me.

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

It would only make sense.Anyway after a bit more study i think the fastest possible way is by using the streambuf class methods directly or the i/ostringstream_iterators. I also could use someone's approval regarding that statement.

caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You