Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

native data  types

Posted on 2001-09-13
6
Medium Priority
?
282 Views
Last Modified: 2013-12-03
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
Comment
Question by:pascal_lalonde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 6479891
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
 
LVL 16

Expert Comment

by:imladris
ID: 6479944
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
 

Author Comment

by:pascal_lalonde
ID: 6480194
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.

 
LVL 11

Accepted Solution

by:
griessh earned 400 total points
ID: 6480290
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
 
LVL 16

Expert Comment

by:imladris
ID: 6480471
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
 

Author Comment

by:pascal_lalonde
ID: 6480536
Thank you very much!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question