• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

native data types

Hello,

  What does native data types mean in C?

  Also I heard that for the execution of a program it's faster to use native types even if in particular cases it means to use a type bigger than what is necessary. By instance, I heard that on a 32 bit microprocessor it's faster to use a 32 bit variable than a char for an alphanumeric variable.

  Please clarify this for me.

Thank you!
0
pascal_lalonde
Asked:
pascal_lalonde
  • 2
  • 2
  • 2
1 Solution
 
griesshCommented:
Native types are types build into the language (like int, char etc.).

A 32bit CPU has the best performance with 32bit data types, that's what it is build for. Depending on the commands a CPU gives you, you could get better performance by using 32bit versions of variables. But good compilers should be able to handle these problems for you. Your example with the character type: If you don't know how your compiler/CPU treads byte variables, you are most likely NOT better of using 4byte variable for characters and deal with the conversions yourself (Imagine taking care of the results of char(0xff) + char(0xff)!)

You need a very good understanding of your CPU/compiler to optimize the code yourself.

======
Werner
0
 
imladrisCommented:
These kinds of things depend on the specific compiler and hardware that is being used. For instance, suppose you wish to use a variable for holding a value indicating true or false. From a memory point of view it seems better to use a char (1 byte) variable for this. However, what the compiler winds up doing is loading the byte into a register. Then, so that it can test the register against 0 (for false) it uses another instruction to clear the upper byte(s). If you loaded a 32 bit variable the second instruction would not be needed.
0
 
pascal_lalondeAuthor Commented:
I'm refering to the sentence where griessh says "But goog compilers should handle this for you"

If I declared  "char toto;" how the compiler will optimize it for a 32 bit microprocessor? Is it better if I declare "unsigned int toto;"?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
griesshCommented:
As I tried to explain to you, it's the operations ON that data type that might be different. I can't tell you how the compiler would optimize, because there is a person who programmed the compiler and how he designs the optimizating depends on him and the CPU.

For your example: You are able to do a
toto = 0xff + 0xff;

For a char (byte) you will go out of range the result might be 0xfe. With an unsigned int the result is 0x1fe, certainly not a char result! YOU have to do work to make that result a char, good luck with that! (I know the example isn't very realistic, but it is very typical for this kind of problem).

In the case of the compiler doing this, there could be a byte operator in the CPU or the compiler could mask on the assembly level with 0x00ff, or ... (your own suggestions go here).

The conclusion: Don't try to be smarter than your compiler. The people programmimg those do that all the time. There might be ery special cases where you can do better, but wait for that case.

For now I suggest to continue using the type char for characters, it certainly is easier to understand :-)

======
Werner

======
Werner
0
 
imladrisCommented:
In general I agree with griessh. Apart from initial broad design consideration (what data structures will I use, how will the program be structured etc. etc.) you should not worry about optimization until the very end of an implementation. In general you should use the datatypes that are appropriate for the use you are making of them. If you need characters, use char, if you need a number, use an int, if you need a really big number, use long int, and for boolean (which is a numeric type) use int. It is important for source code to be as clear and natural as possible. In the real world, people will be maintaining this for a long time.

If, at the end of the implementation, when you have something that runs well, you can consider whether there is any need to do any optimization, and if so, how to go about that. You would probably need a profiler. And again, in general, if there is a performance problem in the code (as opposed to on the network or the disk or whatever) what you will probably find is that there are a very small number of bottlenecks involving some dozens of lines of code that are holding things up. Those then need optimization.

So, this brings us back to griessh's maxim: "don't try to be smarter than your compiler". Use the source language as it is meant to be used. Optimization should be the very last (sic) thing on your mind.

0
 
pascal_lalondeAuthor Commented:
Thank you very much!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now