Sign in to follow this  
mrm83

short vs int

Recommended Posts

mrm83    122
just a quick question. if there is a game program, and the game score has a max value of 5000 and min value of -5000, would it be better to declare it as INT or SHORT? considering the key issue here is to save memory and cpu power. i know that short uses less memory, but i read somewhere that the program will convert it to an int, so using an int would be better since there is no conversion. but then short uses less memory.. ps: this question was actually from a test, and i answered int.. =and i think i might have answered wrong..so i just wanna see what you guys think.

Share this post


Link to post
Share on other sites
UltimateWalrus    229
I'm pretty sure that using shorts will actually save CPU power (as well as memory). It would be ridiculous to convert it to an int for regular short operations --- the CPU is built for that sort of thing. Perhaps a really bad compiler will do this, but if you are using MSVC or similar, it should be fine.

If you don't believe me, write a simple test program and open up a "disassembly" window and look at the assembler code. You don't have to understand it to see if more stuff is going on than there should be.


EDIT: The compiler may, however, automatically convert it to an int when you are doing operations that require it to be this large. For example, if s is a short and i is an int, the expression s+i will probably convert s to an int before evaluating.

EDIT: Also, you can try doing benchmarks on say, a million arithmetic operations, to see if I'm right or wrong. Like I said, I'm pretty sure shorts are faster but I'm not positive.

Share this post


Link to post
Share on other sites
CTar    1134
It depends. If you want to send the data over a network then saving space is your number one concern, however when operating on data locally you couldn't care less about 2 bytes (the most common differences between the two). With optimizations set to high then shorts may get converted into ints under some circumstances, but in general I doubt the compiler is allowed to do that. As soon as you start doing anything that depends on the size then I think it'll keep it a short.

Anyway an int is probably the best type to use anyway. In the standard int is described with: "Plain ints have the natural size suggested by the architecture of the execution environment" That means that the CPU will work faster on ints than shorts, so if you value performance then ints are probably the way to go.

In some more complex cases you have to take more into consideration though. For instance if you as a server is storing every single score then shorts may be more appropriate or if we have a lot of the variables then we have to keep other things about the machine in consideration. For example an array of shorts of size N may fit into the cache (fast memory), but an array of ints of size N may not and in that case shorts are likely to be better. Sometimes you want your end result to be in a certain type and to avoid an expensive conversion somewhere you may want to do all your calculations in that type too (that doesn't really apply to short and int since sign extension is computationally cheap).

Share this post


Link to post
Share on other sites
jpetrie    13159
Use an int. It really doesn't matter. Though the test could have expected you to answer either way for any number of reasons:

A short might be smaller when it occupies storage in memory (RAM). The exact size of a short isn't strictly specified by the C++ standard anyway; it is commonly 16-bits or two bytes. If you're storing lots of values packed together (for example in an array), and the range of those values fits into a short, and space is really a concern, a short is a good option.

In most other cases it simply does not matter. There are lots of subtle issues: in classes, the compiler may pad the data after the short, when the value is stored in a register it will be extended to the size of a register, the compiler may choose to generate code that masks and swizzles a larger value around in and out of memory depending on the native word size of the target machine, et cetera, et cetera, et cetera.

Most of the time it doesn't matter at all.

Quote:

It would be ridiculous to convert it to an int for regular short operations --- the CPU is built for that sort of thing.

The CPU is built for operating on its native word size. If this happens to differ from a short, it may extend the value to a larger size. For example, when placing the value in a register (which is a fixed size; the native size), also when copying from memory the fetch might be for a native-sized amount of memory (say, 32-bits) which is then masked to produce the desired (say, 16-bit) short value. It's rather complex, but ultimately it makes very little difference in the scope of many programs.

Share this post


Link to post
Share on other sites
Palidine    1315
Just to add to the consensus.

Using a short instead of an int will have no positive effect on performance. int is always the native register size (I think that's the standard anyway). Therefore a short will occupy exactly the same amount of space in the CPU registers as an int and processing it will take at least the same amount of time (maybe longer if the masking has a performance cost).

Using a short instead of an int will save memory if: the short is not inside a class or if padding is turned off. However, turning off padding will sometimes decrease performance.

For a single variable like the score it doesn't matter (I don't think there are any game platforms left where 2bytes even registers on the memory footprint). Certainly if you have a few megabytes of data that is stored as int when it could just as easily be stored as short, you may want to go with the short.

There is always a tradeoff between memory and performance. The more memory you use, generally, the faster your program runs. Computers have an upper bound of both memory and performance so you always have to compromise to make the application "fit" on the machine. Some systems become more heavily optimized for memory, others are more heavily optimized for performance.

-me

Share this post


Link to post
Share on other sites
mrm83    122
dang...
maybe i shouldve answered short..

becuase based on the info supplied in the question, the very basics, i think it would be short.

Share this post


Link to post
Share on other sites
TheUnbeliever    963
Quote:
Original post by mrm83
dang...
maybe i shouldve answered short..

becuase based on the info supplied in the question, the very basics, i think it would be short.



I suspect the answer they'd be looking for would be short. I would, IMHO, consider this incorrect. int would give the most efficient size for either 16-bit or 32-bit processors. It's the shortest type name for a reason.

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by mrm83
if there is a game program, and the game score has a max value of 5000 and min value of -5000, would it be better to declare it as INT or SHORT?
...
ps: this question was actually from a test, and i answered int.. =and i think i might have answered wrong..so i just wanna see what you guys think.

I hope that the test giver was more interested in the reasoning behind the answer than the answer itself. Unless the question stated the conditions and the goals very specifically, there is no correct answer. If memory usage overrides all other considerations, then the answer is obviously SHORT. If performance overrides all other considerations, then the answer depends on the CPU (16-bit operations are slower than 32-bit operations on many CPUs). However, in a real situation, the answer would tend toward INT, because there a difference of only 2 bytes at most, there would be no detectable difference in performance, and int would be safer and easier to maintain.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this