I've heard that on a 32-bit CPU all types are converted to integer before they can be used, if this is true it might be better (in terms of speed) to use integers, especially since most people have at least a 1gb of ram.
In general, memory alignment on processors is 4 bytes. x86, to be compatible with older chips, emulates non-word alignment with micro-ops, but it has to load 2 integers and mask/shift them into one. So if you DIM 2 bytes in a row, they take up 8 bytes in memory anyway.
However, having a strongly enforced BOOL type has its benefits though, and something like C++-style enumerations would be a nice feature that could implement that.
jofers wrote:
However, having a strongly enforced BOOL type has its benefits though, and something like C++-style enumerations would be a nice feature that could implement that.
You lost me at c++-style enumerations, i don't know a bit about that language.
So there's no way to access those unused bytes, then?
I think the least memory would be used if you would have 32 settings, use an interger and set each bit of that integer to 0 or 1 coresponding to the 32 settings you need to store :D
If you have a lot of boolean values for a type you could also try bit flags.
You could use an integer to store the options, where each bit of the integer represents an option. A bit is set with the OR operator and can be tested with the AND operator, like this:
enum
OPTION_1 = &b001 '' first bit
OPTION_2 = &b010 '' 2nd bit
OPTION_3 = &b100 '' 3rd bit
end enum
'' If the integer is 0, all bits are 0, i.e. no options set/no flags true.
dim as integer flags = 0
'' Set option.
flags or= OPTION_1
'' Check is a flag is TRUE:
if ( flags and OPTION_2 ) then
print "flag 2 is enabled"
else
print "flag 2 is off"
end if
Type game
isRunning:1 As Integer
isPaused:1 As Integer
End Type
The type still aligns to 4 bytes, but the code is much more readable than masking bitflags.
Oh, and the central difference between C++ enums and FreeBASIC enums is that you can assign any integer value to a FreeBASIC enum. C++ style, this would be illegal:
RayBritton wrote:I've heard that on a 32-bit CPU all types are converted to integer before they can be used, if this is true it might be better (in terms of speed) to use integers, especially since most people have at least a 1gb of ram.
Yes, but the conversions are free. And when moving/copying data, size does matters. Even IF you were right that factor would probably win out.
One doesn't have to artificially compress each record to the smallest size, but inflating it for misplaced "performance" reasons is a bit over the top.
Well, i don't have a ton of switches i want to be booleans, so wasting memory isn't really a problem, but it would certainly make the code more easy to read using them.
---
Thanks everybody. And keep on posting if anything else in this subject comes to your mind.