Table of contents

加快 For...Next 循环Making Faster For...Next Loops

office 365 dev account|上次更新日期: 2018/3/13
2 参与人员

整数使用的内存少于变量数据类型,速度越快的更新。但是,这种差异才明显如果您执行的操作可以有数千。例如:Integers use less memory than the Variant data type and are slightly faster to update. However, this difference is only noticeable if you perform many thousands of operations. For example:

Dim CountFaster As Integer    ' First case, use Integer. 
For CountFaster = 0 to 32766     
Next CountFaster 

Dim CountSlower As Variant    ' Second case, use Variant. 
For CountSlower = 0 to 32766 
Next CountSlower 

上述第一种情况需要稍短时间运行比第二种情况。但是,如果CountFaster超过 32767,则会发生错误。若要解决此问题,您可以更改CountFaster长数据类型,接受更多的整数。一般情况下,越小的数据类型、 更少的时间所需更新。款式是比其等效数据类型稍慢。The first case above takes slightly less time to run than the second case. However, if CountFaster exceeds 32,767, an error occurs. To fix this, you can change CountFaster to the Long data type, which accepts a wider range of integers. In general, the smaller the data type, the less time it takes to update. Variants are slightly slower than their equivalent data type.

© 2018 Microsoft