Deciding which type to use?

Hi there,

I was wondering which is the best type to use, let me explain... When ever i am using an integer for example then I would generally use int (int32) for example

But what if i need to only store either 0 or 1, then is it better to use a Byte type rather than another type or an int?

This does seem pretty logical to me but before making mistakes i thought i would ask.

Basically the rule should be use the smallest int possible or use a byte etc..., takes up less space in memory but what about optimisation, using a byte wouldn't take more time to run than using say an int (int32)??

Just wanted some clarification

Regards

I
ianinspainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
For variable which can be 0 or 1 consider using bool (System.Boolean) type.
However, if you want to use this variable in arithmetic (not logical) expressions, use int or byte.
Using smallest possible type saves memory, but sometimes may reduce performance. For example, processor doesn't like unaligned addresses and array of integers may be handled faster than array of bytes. This depends on situations where this variable will be used.
0
ianinspainAuthor Commented:
Thanks AlexFM,

This is what I thought, so I suppose its a trade off of weather I want to save memory or performance.

Are there any set rules?

Or should i just always use the smallest type possible for the calculation? or just use ints ??

The reduce in performance is worth the save in memory or visa versa?

I
0
AlexFMCommented:
It depends on situation where you want to use this variable.

1) Variable is used in logical expressions ( if ... ). Use Boolean type.

2) Variable is used in math expressions. Use int or byte type.

If you want to use this variable in large arrays, use byte type. For single variable it doesn't matter.

int x[1000000];

or

byte x[1000000];

byte is better. A lot of memory wasted in first case reduces performance.

int x;
byte y;

x += y;
Possibly this performs better if y is int. But difference is so small, that you can see it if you put this statement in some loop.

Additional considerations: not all parts of the program are subject of optimization. For example:

Function1();

for ( int i = 0; i < 1000000; i++ )
{
    Function2();
}

Don't waste your time optimizing Function1. It doesn't matter whether it runs 10 ir 15 ms. However, Function2 must be optimized.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ianinspainAuthor Commented:
Much appreciated!

That cleared up some concerns! Thanks
0
AlexFMCommented:
Processor doesn't like unaligned addresses. Single variable or class member will be aligned to 32-bits boundary. In this case, byte takes the same space and performs exactly like int. But array of bytes is better than array of integers, because array members are not aligned.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.