Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it really matter to distinct between short, int, long?

Tags:

c#

types

In my C# app, I would like to know whether it is really important to use short for smaller numbers, int for bigger etc. Does the memory consumption really matter?

like image 406
Thomas Avatar asked Sep 16 '10 16:09

Thomas


People also ask

Should I use short instead of int?

Using short can conserve memory if it is narrower than int , which can be important when using a large array. Your program will use more memory in a 32-bit int system compared to a 16-bit int system.

Is short more efficient than int?

A CPU works more efficient when the data with equals to the native CPU register width. This applies indirect to . NET code as well. In most cases using int in a loop is more efficient than using short.

Why is long the same size as int?

Compiler designers tend to to maximize the performance of int arithmetic, making it the natural size for the underlying processor or OS, and setting up the other types accordingly. But the use of long int , since int can be omitted, it's just the same as long by definition.


1 Answers

Unless you are packing large numbers of these together in some kind of structure, it will probably not affect the memory consumption at all. The best reason to use a particular integer type is compatibility with an API. Other than that, just make sure the type you pick has enough range to cover the values you need. Beyond that for simple local variables, it doesn't matter much.

like image 147
recursive Avatar answered Sep 22 '22 22:09

recursive