char vs. int
I was wondering whether it would be better to store and use data as char or int, since char uses only 8 bits and you could do stuff like say 'char a = 0x0F;' and then write it to a file, writing specific binary data to store info, or even use it as variables for arithmetic that only required smaller containers as opposed to using 32 bit int. However, I have also read that on 32-bit or 64-bit machines using smaller containers actually causes the program to run slower since it reads and writes in 32-bits, so maybe using an int type would be more efficient?
Basically it comes down to two questions, first is it better to use char in a program as an 8-bit number instead of using a larger int variable when 2^32 values are not needed or is nothing saved like this?
Second, is it better to store data into char variables and write to files in this form, or is it faster to use int 32-bit data type?
Thanks for any input,
Reading and writing values from memory are fastest when done as words (32 bits for 32 bit machines). However, if you are talking about arrays of values (such as bytes), the machine will read a word's worth of values in one chunk and then monkey with them in the registers to isolate the value. Register activity is very fast and even if there are not enough registers to hold all the bits of the values, the remaining ones should be in a local data cache making the subsequent retrieval much faster than the initial retrieval. You should never worry about performance until you have a completely working, fully debugged program. If you like, you can read a bit on performance programming I wrote: http://sol-biotech.com/code/PerformanceProgramming.html.
Thanks, I'll take a look at that