473,587 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'Int' is faster than 'Char'

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

Aug 13 '07 #1
12 9376
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.
Aug 13 '07 #2
On Aug 13, 8:57 am, Ian Collins <ian-n...@hotmail.co mwrote:
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

Aug 13 '07 #3
karthikbalaguru wrote:
On Aug 13, 8:57 am, Ian Collins <ian-n...@hotmail.co mwrote:
>karthikbalagur u 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.
Aug 13 '07 #4
karthikbalaguru <ka************ ***@gmail.comwr ites:
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_Keit h) 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"
Aug 13 '07 #5
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

Aug 13 '07 #6
karthikbalaguru <ka************ ***@gmail.comwr ote:
# 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.
Aug 13 '07 #7
karthikbalaguru wrote:
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)
Whatever the relative speeds might have been, both of these
methods are syntactically incorrect -- for two different
reasons.
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 ?
It depends on how they are used. (Sometimes, when `char` is rather
smaller than `int`, that allows code to run faster, or even run
rather than not, because big boolean arrays won't be as big.)

In any case, such speed variation is a property of /a particular
implementation/, used in /a particular way/, so you can't appeal
to the standard for an answer, and if you want to appeal to
other programmers, you'd better be specific about how you're
using the Methods and what platform you're using and what you're
performance goals are.

--
Semicolons And Spaces For Sale Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Aug 13 '07 #8
karthikbalaguru <ka************ ***@gmail.comwr ites:
[...]
Method 1)
typedef int BOOLEAN
#define TRUE(1)
#define FALSE(0)

Method 2)
typedef char BOOLEAN
#define TRUE(1)
#define FALSE(0)
These definitions are syntax errors. You need a ';' on the typedefs,
and a space after 'TRUE' and 'FALSE', and the parentheses are
unnecessary.

Personally, my favorite way to define a boolean type in C (if _Bool
and <stdbool.h aren't available) is:

typedef enum { false, true } bool;

If you choose to define 'TRUE' and 'FALSE', it's important to remember
*not* to compare a value for equality with TRUE. For example:

BOOLEAN b = some_condition;
if (b == TRUE) /* ... */ WRONG!

Any non-zero value is considered true; the comparison will fail if b
has any value other than 0 or 1. Instead, since b is already a boolean,
just test it directly:

if (b) /* ... */ OK

This is *much* more important than any concerns about performance.

See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
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'.
In addition to fact that the relative performance is system-specific,
it's entirely possible that some operations will be faster for char
than for int, and other operations will be faster for int than for
char.

It's not very likely that the difference either way is going to be
significant. Write clear and correct code. If it works, and it's
fast enough, you're done. If it works, but it's not fast enough, you
can start looking for ways to speed it up. It's easier to make good
code fast than to make fast code good.

--
Keith Thompson (The_Other_Keit h) 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"
Aug 13 '07 #9
On Tue, 14 Aug 2007 09:49:09 +0200, Ravishankar S wrote:

[about using int or char]
In this context it will not matter much since its boolean (usually used as
return type). So not much runtime wasted. But it saves memory (if used for
static or global vars). In one case int is definitely faster than char : On
most RISC processors use of int rather than char for a loop index.
If you don't care about time and want to save space, use char.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 14 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
4662
by: raj | last post by:
Hi, I saw it mentioned that "int" is the fastest data-type for use in C ,that is data storage/retrieval would be the fastest if I use int among the following 4 situations in a 32 bit machine with 4-byte ints: int m; bool m; // assuming I use C++ char m; unsigned char m;
6
1905
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always used this form. Recently, someone asked me why an int was needed, since -1 (usually EOF) can be safely represented in a signed char value. In...
8
3506
by: Bryan Parkoff | last post by:
int has two bytes or four bytes. long has four bytes or eight bytes. I can't be sure to choose int or long keyword because I don't trust to get the wrong size. I always check by using sizeof(...). I always use "short int" to show two bytes and "long int" to show four bytes. It may not be accurate when I port my code from Microsoft C/C++...
78
3842
by: Frederick Gotham | last post by:
On modern 32-Bit PC's, the following setup is common: char: 8-Bit short: 16-Bit int: 32-Bit long: 32-Bit "char" is commonly used to store text characters. "short" is commonly used to store large arrays of numbers, or perhaps wide text characters (via wchar_t).
21
3869
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
43
3847
by: TheDrunkenDead | last post by:
Hello, just wondering how I would assign a char array of four elements to the four bytes used in an int. As of right now my code is: cNameSize = (char)((void)NameSize); cFileSize = (char)((void)FileSize); Where NameSize and FileSize are the integers, and cNameSize and cFileSize are 4 element arrays. This doesn't work.
58
4830
by: LuisC | last post by:
What is better for holding small numbers in a program? I know that char uses less memory, but, with 32 or 64 bits systems, there are advantages (such as processing time) in using int instead of char for small numbers? Luis
10
6168
by: dondora | last post by:
hello~! I'm coding a simple program which demands fast speed. and my cpu is 32bits. which one is faster in processing array and loop? char? int?
13
2790
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.