Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 'Int' is faster than 'Char'

Reply
Thread Tools

'Int' is faster than 'Char'

 
 
karthikbalaguru
Guest
Posts: n/a
 
      08-13-2007
Hi,

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.

Thanks and Regards,
Karthik Balaguru

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-13-2007
karthikbalaguru wrote:
> Hi,
>
> How is 'Int' Faster than 'Char' ?
> I think , 'Char' is small and so it should be easily & efficiently .
>

In which context?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
karthikbalaguru
Guest
Posts: n/a
 
      08-13-2007
On Aug 13, 8:57 am, Ian Collins <(E-Mail Removed)> wrote:
> karthikbalaguru wrote:
> > Hi,

>
> > How is 'Int' Faster than 'Char' ?
> > I think , 'Char' is small and so it should be easily & efficiently .

>
> In which context?
>
> --
> Ian Collins.


Is 'Int' really faster than 'Char' ?

Thx in advans,
Karthik Balaguru

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-13-2007
karthikbalaguru wrote:
> On Aug 13, 8:57 am, Ian Collins <(E-Mail Removed)> wrote:
>> karthikbalaguru wrote:
>>> Hi,
>>> How is 'Int' Faster than 'Char' ?
>>> I think , 'Char' is small and so it should be easily & efficiently .

>> In which context?
>>

*Please don't quote signatures*
>
> Is 'Int' really faster than 'Char' ?
>

In which context? It is significant.

--
Ian Collins.
 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      08-13-2007
On Aug 13, 9:03 am, Ian Collins <(E-Mail Removed)> wrote:
> karthikbalaguru wrote:
> > On Aug 13, 8:57 am, Ian Collins <(E-Mail Removed)> wrote:
> >> karthikbalaguru wrote:
> >>> Hi,
> >>> How is 'Int' Faster than 'Char' ?
> >>> I think , 'Char' is small and so it should be easily & efficiently .
> >> In which context?

>
> *Please don't quote signatures*
>
> > Is 'Int' really faster than 'Char' ?

>
> In which context? It is significant.


Ok, i will tell u the story....
I came accross a document that states that 'int' is faster than
'char'.
So, I would like to know the following because this is the first time,
i am hearing such a stuff -
1) Is 'int' really faster than 'char' ?
2) If so, then how is it ?

I have provided Info found in that doc.
Since 'int' is the fastest data type in C, "Method 1" is better than
any other implementations.

Method 1)
typedef int BOOLEAN
#define TRUE(1)
#define FALSE(0)

Method 2)
typedef char BOOLEAN
#define TRUE(1)
#define FALSE(0)

So, It has been stated 'int' is faster than 'char' .
Of the above two type of methods, Method 2 is found to be slow because
it uses 'char'.
Is it so ? How ?

Thx in advans,
Karthik Balaguru

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-13-2007
karthikbalaguru <(E-Mail Removed)> writes:
> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-13-2007
karthikbalaguru wrote:
> On Aug 13, 9:03 am, Ian Collins <(E-Mail Removed)> wrote:
>> karthikbalaguru wrote:
>>> On Aug 13, 8:57 am, Ian Collins <(E-Mail Removed)> wrote:
>>>> karthikbalaguru wrote:
>>>>> Hi,
>>>>> How is 'Int' Faster than 'Char' ?
>>>>> I think , 'Char' is small and so it should be easily & efficiently .
>>>> In which context?

>> *Please don't quote signatures*
>>
>>> Is 'Int' really faster than 'Char' ?

>> In which context? It is significant.

>
> Ok, i will tell u the story....
> I came accross a document that states that 'int' is faster than
> 'char'.
> So, I would like to know the following because this is the first time,
> i am hearing such a stuff -
> 1) Is 'int' really faster than 'char' ?
> 2) If so, then how is it ?
>

The question is still meaningless. There is nothing in the standard
concerning the speed (how would you define speed?) of a type.

On some systems, operations on a char might be faster than on an int
(say an 8 bit micro with a 16 bit int) on others, they might be
significantly slower if the hardware can't address (or has to go through
hoops to address, or has to mask bits or...) individual bytes of memory.

--
Ian Collins.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-13-2007
On Sun, 12 Aug 2007 21:12:51 -0700, karthikbalaguru wrote:
> I came accross a document that states that 'int' is faster than
> 'char'.

Is it the first answer in www.c-faq.com?
> So, I would like to know the following because this is the first time,
> i am hearing such a stuff -
> 1) Is 'int' really faster than 'char' ?
> 2) If so, then how is it ?
>
> I have provided Info found in that doc.
> Since 'int' is the fastest data type in C, "Method 1" is better than
> any other implementations.
>
> Method 1)
> typedef int BOOLEAN
> #define TRUE(1)
> #define FALSE(0)
>
> Method 2)
> typedef char BOOLEAN
> #define TRUE(1)
> #define FALSE(0)
>
> So, It has been stated 'int' is faster than 'char' .
> Of the above two type of methods, Method 2 is found to be slow because
> it uses 'char'.
> Is it so ? How ?

Operators such as &&, ||, <, == etc. all return an int.
Functions such as isspace(), isupper() etc. all return an int.
So in the second case there will be lots of conversions from int
to char which are likely to slow down the code. Also, on some
machines fetching a char is slower than fetching an int.

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-13-2007
On Sun, 12 Aug 2007 20:53:54 -0700, karthikbalaguru wrote:

> Hi,
>
> 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.
>
> Thanks and Regards,
> Karthik Balaguru

If you have C99, use int_fast8_t in <stdint.h> (or _Bool if it is
what you need) if this bothers you.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      08-13-2007
karthikbalaguru <(E-Mail Removed)> wrote:
# Hi,
#
# How is 'Int' Faster than 'Char' ?
# I think , 'Char' is small and so it should be easily & efficiently .

Depends on the hardware. Memory might be optimised for word
access, with bytes requiring extra hardware or software fiddling.
CPU operations might only accept int sized values, with extra
steps of widenning and thinning chars. Or the hardware could be
optimised for char over int. It really depends on the machine
and the intended customer.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The whole world's against us.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SortedSet faster than Collections.sort() Timo Nentwig Java 6 02-26-2005 01:21 AM
Why .NET is faster than Java Cindi Jenkins Java 1 12-06-2004 03:28 AM
rules engines: faster implementation than methods invocations? NOBODY Java 2 10-30-2004 04:19 AM
Comment : ASP.NET Performs 10 Times Faster than J2EE =?Utf-8?B?Sm9obiBQYXVsLiBBIChNQ1AgSUQjIDMwMTUxNzYp?= ASP .Net 6 07-07-2004 12:46 PM
Anything faster than stat() ? Ken Tucker Perl 1 07-08-2003 06:29 AM



Advertisments