tags:

views:

246

answers:

5

Why is it that for any numeric input we prefer an int rather than short, even if the input is of very few integers.

The size of short is 2 bytes on my x86 and 4 bytes for int, shouldn't it be better and faster to allocate than an int?

Or I am wrong in saying that short is not used?

+9  A: 

CPUs are usually fastest when dealing with their "native" integer size. So even though a short may be smaller than an int, the int is probably closer to the native size of a register in your CPU, and therefore is likely to be the most efficient of the two.

In a typical 32-bit CPU architecture, to load a 32-bit value requires one bus cycle to load all the bits. Loading a 16-bit value requires one bus cycle to load the bits, plus throwing half of them away (this operation may still happen within one bus cycle).

Greg Hewgill
So is there *any* case where a short can be used? I had never thought of this till today, since I guess we are used to using int.
sukhbir
'short' might be used for file formats and network protocols, where you do care about how much space you're using.
Roger Lipscombe
so if loading a 16 bit value also be done in one bus cycle then what will be benefit of using 32 bit.
Vivek
Greg said it **may** happen in the same cycle -- but not necessarily so. Also, it's more machine code generated, larger instruction cache footprint.
intgr
What if a struct consisting of 2 shorts or 2 ints then which one is better and efficient then?
Vivek
@ Roger Or embedded systems. Although I doubt someone is going to program an embedded system in a high language (relatively) like C++.
Matthew
@Vivek: depends on the usage.
jalf
don't really know where you going on the "throwing half the bits away" thing. x86 at least includes instructions for moving 8bit (movb),16bit(movw) and 32bit (movl) values around as well as instructions to move an 8bit values into a various width memory locations (movzbl for instance). on most architectures using any data wide up to the native width of the cpu should execute at the same speed from a cpu cycle standpoint. There are memory footprint and cache alignment reasons it may be beneficial to align data in various ways.
Mark
@Vivek - *"so if loading a 16 bit value also be done in one bus cycle then what will be benefit of using 32 bit"* - apart from the fact that you can store much larger magnitude numbers in an int?
Daniel Earwicker
A: 

The short type is very useful if you have a big array full of them and int is just way too big.

Given that the array is big enough, the memory saving will be important (instead of just using an array of ints).

Unicode arrays are also encoded in shorts (although other encode schemes exist).

On embedded devices, space still matters and short might be very beneficial.

Last but not least, some transmission protocols insists in using shorts, so you still need them there.

Toad
Unicode is not encoded as short. First, you are thinking of `wchar_t`, which is a distinct type, and second, the most common encoding is probably UTF-8 which is byte-sized. UTF16 is the default on Windows, but it's far from being "the" Unicode encoding. And finally, there's also UTF32.
jalf
I was actually thinking of utf16, which in fact really is a short, even though you might name it wchar_t. This encoding it typically used for in memory representations. (As you said windows uses it, java uses it, etc)
Toad
+9  A: 

A 16-bit short makes sense if you're keeping so many in memory (in a large array, for example) that the 50% reduction in size adds up to an appreciable reduction in memory overhead. They are not faster than 32-bit integers on modern processors, as Greg correctly pointed out.

Matthew
A: 

In embedded systems, the short and unsigned short data types are used for accessing items that require less bits than the native integer.

For example, if my USB controller has 16 bit registers, and my processor has a native 32 bit integer, I would use an unsigned short to access the registers (provided that the unsigned short data type is 16-bits).

Most of the advice from experienced users (see news:comp.lang.c++.moderated) is to use the native integer size unless a smaller data type must be used. The problem with using short to save memory is that the values may exceed the limits of short. Also, this may be a performance hit on some 32-bit processors, as they have to fetch 32 bits near the 16-bit variable and eliminate the unwanted 16 bits.

My advice is to work on the quality of your programs first, and only worry about optimization if it is warranted and you have extra time in your schedule.

Thomas Matthews
I was not looking for optimization, I was just curious as to why *short* was never used. And since I could not find any convincing reason wherever I looked, I thought the optimization part was the problem.Thanks for your reply, btw.
sukhbir
+1  A: 

Using type short does not guarantee that the actual values will be smaller than those of type int. It allows for them to be smaller, and ensures that they are no bigger. Note too that short must be larger than or equal in size to type char.

The original question above contains actual sizes for the processor in question, but when porting code to a new environment, one can only rely on weak relative assumptions without verifying the implementation-defined sizes.

The C header <stdint.h> -- or, from C++, <cstdint> -- defines types of specified size, such as uint8_t for an unsigned integral type exactly eight bits wide. Use these types when attempting to conform to an externally-specified format such as a network protocol or binary file format.

seh
You should mention <stdint.h>
Tom
Thanks for the suggestion. I added another paragraph on that topic.
seh
I would upvote you twice if I could (^:
Tom