Sign in to follow this  

question about numeric types

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Floats isn't necessarily slower than integers. These day everyone has highperformance SIMD-extensions that enables you to work floats as fast (or in come cases faster) than integers.
Doubles require more bandwidth, and not all SIMD-processors support them.
short/long integers can differ in speed, although they usually don't. The difference is usually just required bandwidth. When trying to perform 64bit operations on a 32bit cpu, it's ofcourse slower than 32bit though.

Share this post


Link to post
Share on other sites
On most platforms, the speed difference between floating point and integer too small to justify the hassles of conversions.

As far as integer sizes go, I know that on earlier x86 processors, operations on 2-byte operands was slower than 1-byte and 4-byte operands (which were the same). I don't know if that is still the case. Either way, it is probably not worth the hassle. Just use int and be happy.

Share this post


Link to post
Share on other sites
I'll probably get shot down by ASM gurus, so take my words with a grain of salt, but it is my understanding that plain "int" is intended to be the "fast" integer type for your platform. On 16-bit systems, it was equivalent to short. On 32-bit systems, it is equivalent to long.

Share this post


Link to post
Share on other sites
Most of these efficiency concerns probably fall into the realm of unnecessary micro-optimisations. The one thing you may want to watch out for in critical sections is not arithmetic involving either integral or floating-point types, but conversions between integral and floating-point numbers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
I'll probably get shot down by ASM gurus, so take my words with a grain of salt, but it is my understanding that plain "int" is intended to be the "fast" integer type for your platform. On 16-bit systems, it was equivalent to short. On 32-bit systems, it is equivalent to long.


Yes - 32bit CPU likes 32bit types most. And there's true happiness of the CPU if they're aligned on 32bit boundaries (the address is divisible by 4).

So - int and float should be the fastest ones. It requires two CPU instructions to fetch 64bits from memory (double). If it's 32bit CPU, of course.

I do even think, that if you create a short on the stack, the compiler generates some alignment, so it's in fact 32-bit long, but only 16bits are used...

I've created a program that creates a short on stack and prints it out (otherwise it would optimize the variable out totally, probably). It created 4 bytes on stack. I changed the code to int, and it still created 4 bytes. Then I tried double and 8 bytes of space were created. So it looks like, there's no real point for using short, unless you have even number of them (there would be one integer created, but used for two variables).

Oxyd

Share this post


Link to post
Share on other sites
AFAIK in the case of short vs int, the numeric type used does make a difference on X86 (Assuming VC sizes, short 16-bit, int 32-bit).

Since the processor fetches data in 32-bit aligned sections, when it tries to fetch shorts it might try to fetch two shorts on a single DWORD. The packing and unpacking of the shorts in the DWORD can cause a perf hit. This can slow down your application significantly if you are doing a lot of operations on these numeric types. In most cases if is safer to use int, unless you have a very good reason not to (i.e. you might use short on your network layer to save bandwidth)

As always, do not optimize anything if you do not know what is slowing down your application, and always profile before even worrying about stuff like this.


Note that the sizes for short, int, and long are comiler dependent, and the only thing that you are guaranteed is that:
long >= int >= short

Share this post


Link to post
Share on other sites

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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