+ 1
Should I store age in an int or other numeric data types?
Would it be more effective to use byte/short to store ages? Any answer is appreciated 👍
8 Respostas
+ 5
(edited)
byte is 8 bits, ranging from [0; 255]. Would probably suffice for age.
int takes 32 bits each.
So in terms of memory byte would be enough but... depends on what you want do: Are you really going to operate with data in a way in which there would be noticeable differences? That it would be critical for performance?
If not, I would stick to integer because it seems more intuitive to me.
+ 4
If your looking for efficiency and it’s a realistic age, then just use type char.
Char usually is one byte and can hold values -127 to +127 (which should be sufficient for age).
You could use unsigned char to get 0 to 255.
A short is usually 2 bytes and an int 4 bytes.
Thats for C++, for C# you would want to use a sbyte or byte. As char type in C# is
two bytes to accomodate unicode characters.
+ 3
Glad to help, that’s just because chars are stored as integers (ascii or binary)
+ 2
If you have doubt it'll make a difference, what do you mean by "more effective"? (I thought of it in terms of performance)
+ 1
Thank you, DavX 😀 didn't know you could store numbers in char in C++, that's something nice to know
0
Lisa
I'm not sure if it'll make a huge difference if they were to be stored in a database
0
Lisa
In terms of either performance or storage
0
Martin Taylor I did include both C++ and C# and made that clear.
I don’t agree that the most flexible solution is to never store age...
There are plenty of use cases where the age ‘on the day’ is needed.
Take a theme park or swimming pool for example, on admission do you get asked your age, DOB or JDN?
Establishments like these are not interested in tracking your age, all they want to know is your age on the day you visit.
For these cases it’s easier and more efficient to store the age.