Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dwarf with Axe

safe to open a file everyframe?

This topic is 5992 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was just wondering if it was safe (on performance & the processor) if I were to, every frame: open a file, extract information, close file open the file, write some stuff, close it again The files would be small, less than 10kb text files... THe reason I''m asking is because I want to buffer the input of a chat box into a text file. ~Dwarf

Share this post


Link to post
Share on other sites
Advertisement
in short its not advicable, you can surely do this without a txt file, i was a little more awake i''d produce a sample. Perhaps someone else will, otherwise i''ll be back later

Share this post


Link to post
Share on other sites
Why bother? Just keep it in memory. Accessing the harddrive is SLOW, and opening files isnt lightning fast either.

Share this post


Link to post
Share on other sites
dwarf:if you do it everyframe it will be a performace killer. if you do it once a sec it might be acceptable, althrought you should avoid it like fire.

few tips:
1.leave file open if possible
2.read/write all data with one call, don''t perform many read/write operations as they are v.slow. It''s much faster to load everything into memory and work from there.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you *really need* to write to it every frame (more or less), why not keep it open all the time, and flush after every write (or after a few writes to increase performance). That way if your program crashes the file will still contain the data. Only unflushed data would be lost. There is certainly no reason to open it twice each frame (as your description suggests, once to read stuff and once to write. Just open it in modify mode allowing you to both read and write).

You will of course still lose a lot of performance if you write to a file every frame, but at least you won''t incur the overhead of opening and closing the file all the time.

The only valid reason I can think of to do anything like what you''re doing is that you''re afraid of losing data in case the game crashes (though a chat-log doesn''t sound like very critical data). If that''s the case, just make your game more stable .

Whatever the reason you should probably rethink the whole thing anyway. Keep your data in memory and save it as infrequently as you can. Preferably at times where speed isn''t critical, like when loading a new area/level, or when showing some menu, or waiting for some kind of non-realtime user interaction (e.g. during click-through dialog, or similar, if your game has anything like that). I''m sure you can find places in your game where it''s ''safe'' to save. Additionally you can have a time limit and/or size limit for the buffered data and save when one of the limits is reached (though your users are fairly likely to experience glitches and ''stuttering'' graphics when you do).

Share this post


Link to post
Share on other sites
Bah! I guess all those years of good code practive let me slip on that one..


I never even thought about keeping it open the whole time... Is that a logical thing to do?

BTW, its not only chat stuff, but dynamic information thats going to be written to the file anyway... Just, accessing _it_ instead of a memory buffer seems easier...

Do the yay-or-nay on leaving it open sounds like a yay, aye?

~Jesse

Share this post


Link to post
Share on other sites
You could of course keep it in memmory and only write it to a file every 15 minutes or when your program exit''s.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Dwarf with Axe
I never even thought about keeping it open the whole time... Is that a logical thing to do?

Sure.

Though, IMHO you should really reconsider the whole scheme. Why does it seem easier to write the stuff to a file rather than keep it in memory?

Share this post


Link to post
Share on other sites
Yeah you, must really be doing something wrong to have to write data every frame... just keep it in memory and write when your done! I cant see whats so hard about this...

CEO Platoon Studios

Share this post


Link to post
Share on other sites
If there is no reason to persist the information (save it outside the current game) then don''t bother making a text file, as the ppl above have mentioned it is a performance killer. If you want be able to look back on what has been said (like a chat history, saving the last 20 posts or whatever) I would use a vector or list of strings.

Free advice is often worth as much as it costs, remember that before flaming.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!