what's the fastest way to write data into a file?
cout or fprintf?
Is there any even faster way to do this?
what's the fastest way to write data into a file?
cout or fprintf?
Is there any even faster way to do this?
The fastest I/O stuff is pretty much always OS-specific. For example, write() on Unix-based systems.
I am using WinXP
I imagine that functions which do not do any type parsing would be faster than those which do, such as fprintf(). So fputs() might be a good idea. Though cout.operator<<(const char *) might be just as fast.
Those are just the standard C and C++ functions. I haven't considered the Windows-specific ones.
How important is this speed to your program? Are you willing to sacrifice portablity for a few milliseconds' execution speed? Why do you need fast I/O?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
As already said, the fastest way is to use system calls.
WriteFile() for Windows. write() for Unix/Linux.
Well, I used fprintf in my current program. The program opens a file frequently, write something into it, and then close it. I just wanted to see if I can improve speed by using another output function.
If there is not such a good function, I have to consider about printing the data into a string, and printing the string out when the program ends. Will that save a lot of time?
It might save some time. Would it be difficult to program?If there is not such a good function, I have to consider about printing the data into a string, and printing the string out when the program ends. Will that save a lot of time?
It sounds like your program might be taking a lot of time to open files over and over again. You might be able to save lots of time by only opening the file once. Assuming that you're only writing to one file.
How much data are you writing, and how long does your program take to run?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Sorry for late response, I have just been too absorbed in programming.
I need to run the program for thousands of times, and one run may takes around 40 mins. The output file size, as I could estimate now, is roughly 20~200k.
so any suggestions for optimization?
It happens to all of us.Sorry for late response, I have just been too absorbed in programming.
Wow, yes, then definitely go for whatever speed increases you can get away with.I need to run the program for thousands of times, and one run may takes around 40 mins. The output file size, as I could estimate now, is roughly 20~200k.
so any suggestions for optimization?
But one run, generating a 20-200KB file, takes 40 minutes? You must either have a very slow computer, or the program must do a lot of extra work. How much of that time is actually spent on file I/O?
I suggest using a profiler to see which portions of your code are taking excessive amounts of time. With GCC, you can use gprof.
You should be able to figure out the output from gprof. If not, do some searching or post it here.Code:$ gcc -pg hello.c -o hello.exe $ ./hello.exe $ gprof hello.exe > gprof_output1
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Taking that long has nothing to do with the method used to write to file.
The reality is, you have no idea why it takes so long and are just picking one thing and asking how to speed up that one thing. That wont get you anywhere.
You would have to make available a lot of the code that is so slow. Right now we can't do squat to help you.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
> The output file size, as I could estimate now, is roughly 20~200k.
If it takes 40 minutes to produce 200K of output, then I/O is NOT your problem.
Try writing a program which just generates 200K of output using cout or printf, and see how long it takes. You'll barely be able to get your finger off the return key before it's done.
Which compiler are you using?
If it's gcc based, then look into something called 'gprof', which is a profiler.
This will tell you exactly where the code is spending the most time, and those are the areas you need to focus on.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
hmm thousands = at least 2I need to run the program for thousands of times, and one run may takes around 40 mins.
2000 *40min / 60min/hr / 24h/day = wtf! 55.55 days, hope you got a snickers :-)