karthikbalaguru <ka***************@gmail.comwrites:
How is 'Int' Faster than 'Char' ?
I think , 'Char' is small and so it should be easily & efficiently .
Can someone here provide some info regarding this.
C is a case-sensitive language. 'Int' and 'int' are two different
identifiers (actually an identifier and a keyword), as are 'Char' and
'char'. So your question is:
How is 'int' faster than 'char'.
int and char are types. Types do not have speed in any meaningful
sense. Specific operations on types might be faster or slower than
specific operations on other types.
So your real question is something like:
How are operations on operands of type 'int' faster than the same
operations on operands of type 'char'.
As far as the C language is concerned, the question is *still*
meaningless. The language doesn't say anything about that the
relative speeds of different operations, and such things can and often
do vary from one implementation to another.
On *some* systems, operations on 'int' can indeed be faster than
operations on 'char', because the hardware is designed to operate
efficiently on 'word'-sized chunks of data. For example, suppose int
is 32 bits, and char is 8 bits (other sizes are possible). The CPU
might have instructions to load, store, and operate on 32-bit chunks
of data. It might not have such instructions for 8-bit data; instead,
to load an 8-bit value, it might have to load a 32-bit value and
extract the desired 8 bits. To store an 8-bit value, it might have to
load 32 bits, use bitwise operations to set the desired 8-bit subset,
and then store 32 bits.
On other systems, operations on 8-bit values might be faster than
operations on 32-bit values. Or they might be exactly the same speed.
The standard (C99 6.2.5p5) says:
A "plain" int object has the natural size suggested by the
architecture of the execution environment ...
This suggests, but does not require, that an int is typically one
"word", and that operations on it are likely to be efficient.
If you want to have a single object, or a few individual objects,
intended to hold only small values, it's likely (but by no means
guaranteed) that using 'int' will give you the fastest code. Using
'char' instead might increase the size of your code more than it
reduces the size of your objects.
On the other hand, if you want large arrays of such objects, the space
saved by using 'char' could be more important.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"