Skip to main content
gamedev.net
🔒 Locked

Text vs Binary 5mb File

Started by ViperG Aug 3, 2006 at 5:55 PM 1 replies 500+ views
Original Post
ViperG
ViperG
I know this question is asked alot, but im looking for a specific answer. and i tried googling it and found 1 answer. I just want to confirm, make sure its accurate information. Basicly, anything below 1 meg, binary wont really beat a text file. Say i had a 5 meg text file. and its mostly stuff like size= 582 var1= 2 var3= 5 How much faster would binary be over this text file? (if the file was binary instead)
Black Sky A Star Control 2/Elite like game
Limitz
Limitz
Quote:
Original post by ViperG
size= 582
var1= 2
var3= 5


Lets say that you store each key value as a struct with a char to identify between 256 possible keys, and a short int to assign a numeric value of 16 bits max. Then your filesize would be 3 bytes * number of key-value pairs. whereas your text based file holds 4 bytes per key, 2 for "= " and up to 6 bytes for a 16 bit numeric., so you'd have 7 to 12 bytes per key value, which is 4 times as much in worst case.

On top of that you also have to read and parse your text, while with binary you can just fread and fwrite an entire array of your key value struct into the memory or back into the file, which is a lot faster.

Does this answer your question?
DrEvil
DrEvil
binary is pretty much always faster to read in, cuz typically you can read it right into your struct, or even right into an array of structs. textual parsing is usually done line by line or by token or something, and text has alot of additional overhead to make it readible to people that binary doesnt.

consider your example

size= 582
var1= 2
var3= 5

around 10 + 8 + 8 bytes for those 3 lines for text. ~26 bytes
for a binary file that could be 12 bytes, or 3 ints, or if you didn't need the range of an int it could be 3 shorts, or 6 bytes. if you really wanted to press small files you could further reduce by optimizing down to the bit level.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.