943,920 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1667
  • C++ RSS
Jun 17th, 2009
0

Which is faster/more efficient c++´s fstream, or c's FILE

Expand Post »
Hi All,

Does anyone know which file manipulation library is faster between c's FILE and c++'s fstream?

I would suppose that c++'s fstream would be the faster but I just wanted to see some expert advice/opinion on this.

I googled the internet without finding anything concrete.

If there is any existing thread on this I would appreciate if you could post it here so I can take a look.

Thanks for your help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
george_cpp is offline Offline
2 posts
since Jun 2009
Jun 17th, 2009
3

Re: Which is faster/more efficient c++´s fstream, or c's FILE

On current implementations, you'll probably find that the stdio library is faster than the iostream library. The usual recommendation is to try both and use the one that's fastest on your setup, if all you care about is execution performance.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 17th, 2009
0

Re: Which is faster/more efficient c++´s fstream, or c's FILE

Click to Expand / Collapse  Quote originally posted by Narue ...
On current implementations, you'll probably find that the stdio library is faster than the iostream library. The usual recommendation is to try both and use the one that's fastest on your setup, if all you care about is execution performance.
Thanks Narue,

I will try to make a test using timers for both once I have the time, Ill post the results with some code for any interested readers when I do.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
george_cpp is offline Offline
2 posts
since Jun 2009
Jun 17th, 2009
4

Re: Which is faster/more efficient c++´s fstream, or c's FILE

Except which one is "fastest on the current setup" is a moving goalpost with every patch / update / release / new compiler.

Plus if you're writing code which is in any way open source, then a whole raft of different compiler options will appear, each with their own "x is faster than y by z%" metrics.

If you don't already have any need for any C I/O, then you might avoid some code bloat by not dragging in libraries which you're not making a great deal of use of already, for what may be a marginal benefit.

File I/O is terribly slow anyway. You might want "cheetah", but all you've got is a choice between "snail" and "tortoise".

Here's an example. If your C++ I/O takes 1 hour, and your C I/O takes 50 minutes, there isn't any point in making the change. Your users are NOT going to come back from lunch 10 minutes earlier due to your efficiency.

Likewise, if it's around 10 minutes, then a few minutes either way won't get them back from the coffee break any time sooner.

If you're going to do it, one of the methods needs to break through a time barrier where your users would figure out they could go and do something more productive while your program was running.

An hour to a minute say needs a change of algorithm, not a change of I/O library.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 17th, 2009
1

Re: Which is faster/more efficient c++´s fstream, or c's FILE

Oh please. Micro-optimization is not cool. Adhere to the philosophy that "Developers time is far far more important than machine time"
Small optimization won't make significant changes in the execution but will drastically increase your head ache.
I find Salem post to be extremely right in this regard. Very well said.
My advice is : use the one which will save you headache while debugging a month( or perhaps a fortnight) later.


>if it's around 10 minutes, then a few minutes either way won't get them back
>from the coffee break any time sooner.
I can have two cups in those 10 minutes.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jun 17th, 2009
0

Re: Which is faster/more efficient c++´s fstream, or c's FILE

I think it depends on what you are doing with the file.
I've found that the FILE, strtok is by far the fastest way of reading in data.
I've been using flat files, (same number of columns in all lines).
On the scale of several gigabytes.

And avoiding the c++ streams was faster.
But I've heard rumors that you can tweak the fstream with a fsetbuf,
that should make it faster.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Shut Down function not working
Next Thread in C++ Forum Timeline: LinKer Error for Static Function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC