Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,620 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,963 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Word sizes and optimization

 
Reply to this topicStart new topic

Word sizes and optimization

Tom9729
post 18 Jul, 2008 - 09:39 AM
Post #1


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,428



Thanked 10 times

Dream Kudos: 325
My Contributions


Just something I've read and would like clarified...

QUOTE
use word sized variables if possible

I've been using the smallest sized variables possible because I thought it saved memory (ie. 8 and 16 bit integers). Should I be using 32 bit integers instead, since I have a 32 bit processor? Should I add macros to use 64 bit integers on 64 bit processors? blink.gif

User is online!Profile CardPM

Go to the top of the page


Cerolobo
post 18 Jul, 2008 - 10:13 AM
Post #2


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 30 times
My Contributions


Yes, using smaller types will typically reduce the memory; however, it highly depends.

Take the stack, for example. Everything on the stack is actually the native size of the CPU. So, if you just have a char in a function, you really didn't save any memory at all.

Compilers will typically add padding to structures as well, to promote the size of the struct to a multiple of the CPU bit size. They usually do this, since it'll allow the data to be copied in far less cycles then a non-aligned struct.

I personally like to just use the standard types (char, short, int, long, ect...), since I typically find that it doesn't really matter. As long as you can guarantee that the type you choose can hold the values you need, you are fine.

One of the common things I see in open source projects, that I hate, is the use of specific type sizes (originally I wasn't going to talk about this, but since you brought it up..). Take int32_t, for example. In nearly every situation I've seen, there was never a need for 32bits integers. In fact, more then likely, a 16bit integer is more then enough. The problem with defining the number of bytes, is that it does not scale at all. On a 16bit CPU, int32_t is now a very slow type. Plus, the 16bit CPU is far more limited in things like memory, so those extra 16bits aren't going to do you any good. Now take a 64bit cpu. Again, int32_t is a slower type. Plus, here are you limiting your program to handle up to 32bits.

If you just use the native type, and let the compiler handle size restraints, then your program will scale far better. Not to mention that it'll generally have better performance.

Here is a general table of the types, and what I use them for

CODE
char: At least 8 bits
       Great for saving space in arrays, if 256 values is enough
short: At least 16 bits
       Same as above, but up to 65535 values
  int: Native size of the CPU
       Great when performance matters
long: At least 32bits
       Great for handling files in 16bit environments


A WORD is typically the size of the CPU bits. IE, that translates to a int, which does scale far better.

This post has been edited by Cerolobo: 18 Jul, 2008 - 10:16 AM
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 18 Jul, 2008 - 11:10 AM
Post #3


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,428



Thanked 10 times

Dream Kudos: 325
My Contributions


Thanks, that was a really good explanation.

I didn't know that about fixed-width integer types. I'm probably not going to go through Dagger3d and switch all of them, but I'll keep what you said in mind for future projects. icon_up.gif
User is online!Profile CardPM

Go to the top of the page

GhostlyDeath
post 19 Jul, 2008 - 05:13 AM
Post #4


New D.I.C Head

*
Joined: 11 Jul, 2008
Posts: 15


My Contributions


QUOTE(Cerolobo @ 18 Jul, 2008 - 10:13 AM) *

Here is a general table of the types, and what I use them for

CODE
char: At least 8 bits
       Great for saving space in arrays, if 256 values is enough
short: At least 16 bits
       Same as above, but up to 65535 values
  int: Native size of the CPU
       Great when performance matters
long: At least 32bits
       Great for handling files in 16bit environments



Depends on the compiler, On GCC 64-bit, long is 64-bits but on MSVC++ it's 32-bits, int on Borland C++ 3 is 2 bytes and long is 4. int now is pretty much 32-bits now except on ancient 16-bit compilers. If you want to be portable in sizes use the fixed types such as int32_t/__int32.

User is offlineProfile CardPM

Go to the top of the page

Cerolobo
post 19 Jul, 2008 - 06:49 AM
Post #5


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 30 times
My Contributions


QUOTE(GhostlyDeath @ 19 Jul, 2008 - 05:13 AM) *
Depends on the compiler, On GCC 64-bit, long is 64-bits but on MSVC++ it's 32-bits, int on Borland C++ 3 is 2 bytes and long is 4. int now is pretty much 32-bits now except on ancient 16-bit compilers. If you want to be portable in sizes use the fixed types such as int32_t/__int32.


Ok, I should rephrase that. It really does depend on the data model the compiler is using.

for 16 bit, ints are typically 16 bits
for 32 bit, ints are typically 32 bits

64 bit is where it starts to get messy. The bulk of the 64 bit compilers I've seen, use the LP64 data model. Wikipedia actually has a decent article on 64 bit data models

http://en.wikipedia.org/wiki/64-bit#64-bit_data_models

My point is that using int32_t and similar don't really give you any benefit. If you explicitly require 32 bit variables, then yes, you should use it. However, more then likely, you don't. If you do use the standard type, the data types will more then likely scale with your program.

32 bit and 64 bit are very common today. If you are going to target these platforms, then using long in place of int will allow the variable to scale automatically to your platform (assuming LP64 data model). In 16 bit, however, it will be a slower type.

If you really do care about the size of your data types, you should use your own defines. To me, using things like int32_t tells me that you are only designing your program to run on a specific platform.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/8/08 12:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month