什么是更好的? char还是int? [英] What is better? char or int?

查看:90
本文介绍了什么是更好的? char还是int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序中保存小数字更好吗?

我知道char使用的内存更少,但是,使用32或64位系统时,

使用int而不是

char对于小数字有优势(例如处理时间)?


Luis

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

推荐答案

在文章< 11 ********************* @ q2g2000cwa.googlegroups.c om> ,

LuisC< lu ***************** @ gmail.comwrote:
In article <11*********************@q2g2000cwa.googlegroups.c om>,
LuisC <lu*****************@gmail.comwrote:

>在程序中保存小数字有什么好处?
>What is better for holding small numbers in a program?


>我知道char使用更少的内存,但是,使用32或64位系统,
有优势(例如处理时间)使用int代替
char为小数?
>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?



排序。但是:


a)char和iint允许大小相同。 sizeof(int)可以是1.

显然这会发生在某些DSP上。所以你不会 - 通过使用char来保存
内存。通常。


b)在任何特定系统上访问char或int是否更快

取决于架构。对于现代架构而言,总是获取一个完整的单词并从单词

(处于非常低的水平)中选择char,而不是单独的逻辑,这种情况并不少见。阅读

a单一字符。可以一次读取字符的现代架构有时必须绕过缓存并运行特殊的总线周期,因为内存硬件将只加载所需的字符。

通常提供一个完整的单词。但它随系统的不同而不同,所以

虽然我们可以说这些天char可能比int更慢'/ b
你必须非常彻底地了解你的系统以确保

(它可能会随着下一个编译器版本而改变。)


所以,总是如此:如果它事,衡量。 (然后抛出那些

测量结果并在comp.benchmarks中暂停一段时间以便

来了解你的天真基准测试的17种不同方式
没有测量你认为它在测量的东西!)

-

编程就是在你忙于制定其他计划的时候发生的事情。 />

Sort of. However:

a) char and iint are allowed to be the same size. sizeof(int) can be 1.
Apparently this happens on some DSPs. So you will not -always- save
memory by using char. Just usually.

b) whether char or int is faster to access on any particular system
is architecture dependant. It is not uncommon for modern architectures
to always fetch a full word and pick the char out of the word
(at a very low level), instead of having seperate logic to read
a single char. And modern architectures that can read a char at a time
sometimes have to bypass the cache and run special bus cycles that
load only the required character, because the memory hardware would
normally supply a full word. But it varies with the system, so
although we can say that "These days char might be slower
than int," you have to know your system very thoroughly to be sure
(and it might change with the next compiler version.)

So, like always: if it matters, measure. (And then throw out those
measurements and go hang out in comp.benchmarks for awhile in order
to learn about the 17 different ways in which your naive benchmark
was not measuring what you thought it was measuring!)
--
Programming is what happens while you''re busy making other plans.


LuisC写道:
LuisC wrote:

什么比在程序中保存小数字更好?
What is better for holding small numbers in a program?



int。


我有一条规则:为此目的,不要使用char甚至是short />
,除非考虑内存或某些大数据库。


很久以前,我确实关心过这个......今天,我不是。特别是对于函数

参数。

int.

I have one rule: just don''t use char or even short for this purpose
unless memory is concerned or in some big DB.

Long ago, I did care of this... today, I don''t. Especially for function
arguments.


我知道char使用更少的内存,但是,使用32或64位系统,

使用int而不是

char对于小数字有优势(例如处理时间)?
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?



int应该是原生理想的整数。这是你在处理简单数字时必须为性能原因选择的那个


出于记忆原因选择其他类型(短或char)。


仍然在性能一章中,一些架构必须做更多的操作来正确处理char,其中int才能正常运行

他的拥有(因此更快)。


-

RN

int is supposed to be the "native-ideal" integer. That''s the one you
must choose for performance reasons when dealing with simple numbers.
Choose the other types (short or char) for memory reasons.

Still in the chapter of performance, some architectures must do more
operations to handle char correctly where int would just work well on
his own (thus faster).

--
R.N.




LuisC写道:

LuisC wrote:

在程序中保存小数字更好吗?


我知道char使用较少内存,但是,使用32或64位系统,

使用int而不是

char对于小数字有优势(例如处理时间)?
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?



在C中,char是字符文字和字符串的合适类型,

(在后一种情况下,作为char数组) 。如果您的价值需要比16个值位更多的价值,请使用long。如果它需要超过32位,则需要很长时间

。 16位以下的任何东西都保证适合int $ / b $ b,这可能是最好的选择,除非空间是一个真正的问题。访问char是否慢于int是平台和

编译器相关。在大多数情况下,这是一个过早的优化b b $ b的情况。另请注意,是否签署了普通字符或

unsigned是否已指定实现。

In C, char is the appropriate type for character literals and strings,
(in the latter case, as an array of char). If your value needs more
than 16 value bits, use long. If it needs more than 32 bits, long long
is neccessary. Anything under 16 bits is guaranteed to fit in an int
and that would probably be the best choice, unless space is a real
issue. Whether access to char is slower than int is platform and
compiler dependent. In most cases, it''s a case of premature
optimisation. Note also that whether a plain char is signed or
unsigned is implementation specified.


这篇关于什么是更好的? char还是int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆