ints & shorts

  • Thread starter Thread starter chesca
  • Start date Start date
C

chesca

Hi everyone.


I'm reading "Programming C#" and I just got to the part about built-in data
types. Apparently, integers are better than smaller types because modern
CPUs are optimized for dealing with them. Also, because of padding which is
inserted for alignment, there's no memory gain to be had. Is this all true?
If so, why are there smaller types?

Thanks.
 
chesca said:
I'm reading "Programming C#" and I just got to the part about built-in data
types. Apparently, integers are better than smaller types because modern
CPUs are optimized for dealing with them. Also, because of padding which is
inserted for alignment, there's no memory gain to be had. Is this all true?
If so, why are there smaller types?

Padding isn't universally applicable. The most obvious example is
arrays - an array of byte[] with 1024 elements will take 1K in memory
(plus a tiny, constant, bit of overhead). An array of int[] with 1024
elements will take 4K in memory.

I'd say the main reason, however, is simply because it's more logical
that way. If I only want to represent a byte, I shouldn't be forced to
use an int. It's probably true that byte and short are used less
frequently than int, but I'm glad they're there.
 
Hi everyone.

I'm reading "Programming C#" and I just got to the part about built-in data
types. Apparently, integers are better than smaller types because modern
CPUs are optimized for dealing with them. Also, because of padding which is
inserted for alignment, there's no memory gain to be had. Is this all true?
If so, why are there smaller types?

Thanks.

As Jon already mentioned there is a significant memory difference.
I'm a little suspicious of the CPU performance claim as well. It
seems to me that in some cases the JIT compiler could, for example,
slam 2 shorts into one register and perform a single instruction as
opposed to using 2 ints and having to perform two instructions to get
the same result.
 
chesca said:
Hi everyone.


I'm reading "Programming C#" and I just got to the part about built-in data
types. Apparently, integers are better than smaller types because modern
CPUs are optimized for dealing with them. Also, because of padding which is
inserted for alignment, there's no memory gain to be had. Is this all true?
If so, why are there smaller types?

Thanks.

Although using an int is faster for just one value, there are situations
where a smaller data type can be faster.

If you have an array of bytes it may be faster to access than an array
of ints in some situations, because the entire byte array might fit in
the CPU cache, while the int array would be too large and require more
memory accesses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top