Link to home
Start Free TrialLog in
Avatar of goondoo27
goondoo27

asked on

How often can I call CFile::Flush(), or how often should I?

I have a program that saves data using a CFile object. I call Write() followed by Flush() every time data is available to be saved. My question is, how often should I be calling Flush()? My program can call the Write() function up to 100 times a second. Is it ok to call Flush() 100 times a second? Will this cause any problems? My program is hanging and I'm looking for likely places where the performance could be problematic.
Avatar of jkr
jkr
Flag of Germany image

You can of course call it as often as you want, the performance penalty should be minimal - if there is no data to be written, the function will just return without doing anything. Depending on what kind of data you are writing. You however might want to consider opening the file as 'unbuffered' , so you don't even have to explicitly 'flush' any file buffers.
Avatar of goondoo27
goondoo27

ASKER

Thanks jkr. There will be data to flush every time I call Flush() because I only call it after a Write(). I'll look at opening the file as unbuffered. So you don't see any performance issue with calling Flush() right after Write() at say 100 to 300 times a second?
Again, that will depend on the amount of data you are writing. Also, why do you need everyhing to be written immediately? I'd say that flushing all buffers is a good thing to do when a coherent point is reached, e.g. if you were writing audio data, you'd call 'Flush()' when a full song/track is ready (yes, not really a perfect example)...
I'm writing a text file. Each Write() will put a single line of statistical data into the file. I'd estimate that up to 200 characters are written each time I call Write(). I originally put the Flush() call in the code so that the stats were immediately saved to the file and could be viewed by programs tailing the text file. Is there a better way to do this?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial