Deciding which type to use?

Posted on 2004-11-18
Medium Priority
Last Modified: 2010-04-15
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


Question by:ianinspain
  • 3
  • 2
LVL 48

Expert Comment

ID: 12615137
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.

Author Comment

ID: 12615220
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?

LVL 48

Accepted Solution

AlexFM earned 1000 total points
ID: 12615367
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];


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:


for ( int i = 0; i < 1000000; i++ )

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

Author Comment

ID: 12615450
Much appreciated!

That cleared up some concerns! Thanks
LVL 48

Expert Comment

ID: 12615457
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.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question