Solved

native data  types

Posted on 2001-09-13
6
268 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
  • 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Accepted Solution

by:
griessh earned 100 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql how to exclude from a table 3 73
count8 challlenge 13 85
array6 challenfge 6 62
C Programming - If Statement 8 48
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now