为什么使用整数而不是长? [英] Why Use Integer Instead of Long?

查看:177
本文介绍了为什么使用整数而不是长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常会看到与<$ h $ => / questions / tagged / vba相关的溢出错误的问题.class =post-tagtitle =show questions tagged 'vba'rel =tag> vba 。

I often see questions relating to Overflow errors with vba.

我的问题是为什么要使用整数变量声明而不是仅仅定义所有数值变量(不包括 double 等)为 long

My question is why use the integer variable declaration instead of just defining all numerical variables (excluding double etc.) as long?

除非您执行类似于for循环的操作,您可以保证该值不会超过32,767的限制,否则会影响性能或其他不会使用 long

Unless your performing an operation like in a for loop where you can guarantee that the value won't exceed the 32,767 limit, is there an impact on performance or something else that would dictate not using long?

推荐答案


存储整数变量作为32位(4字节)数字

Integer variables are stored as 32-bit (4-byte) numbers

msdn


长(长整数)变量les存储为带符号的64位(8字节)数字

Long (long integer) variables are stored as signed 64-bit (8-byte) numbers

msdn

因此,好处是减少了内存空间。一个整数占用了一半的内存。现在,我们讨论的是2个字节,所以除非你存储一个TON的整数,否则它不会产生真正的区别。

So, the benefit is in reduced memory space. An Integer takes up half the memory that a long does. Now, we are talking about 2 bytes, so it's not going to make a real difference unless you're storing a TON of integers.

32 位系统上, 16位整数可以无声地转换为长整数,而不需要使用更大范围的数字。溢出仍然发生,它需要同样多的内存。 性能甚至可能 伤害 因为必须转换数据类型(处于非常低的水平)。

BUT on a 32 bit system, a 16 bit integer gets silently converted to a long without the benefit of the larger range of numbers to work with. Overflows still happen and it takes just as much memory. Performance may even be hurt because the datatype has to be converted (at a very low level).

不是我想要的参考但是。 ...

Not the reference I was looking for but....


我的理解是底层VB引擎将整数转换为long,即使它被声明为整数。因此,可以注意到轻微的速度降低。我相信这已经有一段时间了,也许这也是为什么上面的陈述,我没有要求推理。

My understanding is that the underlying VB engine converts integers to long even if its declared as an integer. Therefore a slight speed decrease can be noted. I have believed this for some time and perhaps thats also why the above statement was made, I didnt ask for reasoning.

ozgrid论坛

这是我要找的参考。


简短回答,在32位系统中,2字节整数转换为4字节
Longs。实际上没有其他方法可以使各个位正确排列
以进行任何形式的处理。请考虑以下内容

Short answer, in 32-bit systems 2 byte integers are converted to 4 byte Longs. There really is no other way so that respective bits correctly line up for any form of processing. Consider the following

MsgBox Hex(-1) = Hex(65535) ' = True

显然-1不等于65535但计算机正在返回正确的
答案,即
FFFF= FFFF

Obviously -1 does not equal 65535 yet the computer is returning the correct answer, namely "FFFF" = "FFFF"

然而,如果我们强制-1到很长一段时间我们会得到正确的
答案(65535大于32k自动长)

However had we coerced the -1 to a long first we would have got the right answer (the 65535 being greater than 32k is automatically a long)

MsgBox Hex(-1&) = Hex(65535) ' = False

FFFFFFFF=FFFF

"FFFFFFFF" = "FFFF"

一般来说,VBA没有必要申报现代
系统中的As Integer,除了可能会收到
整数的传统API之外。

Generally there is no point in VBA to declare "As Integer" in modern systems, except perhaps for some legacy API's that expect to receive an Integer.

pcreview论坛

最后我发现 msdn文档我真的很想找。

And at long last I found the msdn documentation I was really truly looking for.


传统上,VBA程序员使用整数来保存小的
数字,因为它们需要更少的内存。在最近的版本中,
但是,VBA将所有整数值转换为Long类型,即使它们是
声明为Integer类型。因此,使用Integer变量不再具有性能优势
;实际上,Long变量可能会稍快
,因为VBA不必转换它们。

Traditionally, VBA programmers have used integers to hold small numbers, because they required less memory. In recent versions, however, VBA converts all integer values to type Long, even if they're declared as type Integer. So there's no longer a performance advantage to using Integer variables; in fact, Long variables may be slightly faster because VBA does not have to convert them.

所以,总之,这几天几乎没有理由使用 Integer 类型。 除非 ,否则需要使用需要16位int的旧API调用进行Interop。

So, in summary, there's almost no good reason to use an Integer type these days. Unless you need to Interop with an old API call that expects a 16 bit int.


值得注意的一点是,一些旧的API函数可能需要16位(2字节)整数的参数,如果你是32位并试图通过引用传递一个整数(已经是一个4字节长),由于字节长度的不同,它将无法工作。

One thing worth pointing out is that some old API functions may be expecting parameters that are 16-bit (2-byte) Integers and if you are on a 32 bit and trying to pass an Integer (that is already a 4-byte long) by reference it will not work due to difference in length of bytes.

感谢Vba4All指出这一点。

Thanks to Vba4All for pointing that out.

这篇关于为什么使用整数而不是长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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