VB6 - 在 VB6 中使用固定宽度字符串是否可以获得任何性能优势?
在 .NET Visual Basic 之前的版本中,程序员可以将字符串声明为特定宽度。例如,我知道社会安全号码(在美国)始终是十一个字符。因此,我可以声明一个字符串,将社会安全号码存储为十一个字符的字符串,如下所示:
Dim SSN As String * 11
我的问题是:这是否会带来任何类型的性能优势,使代码运行得更快或可能使用更少的内存?另外,固定长度字符串在内存中的分配是否会有所不同(即:在堆栈上而不是在堆中)?
In pre-.NET Visual Basic, a programmer could declare a string to be a certain width. For example, I know that a social-security number (in the US) is always eleven characters. So, I can declare a string that would store social-security numbers as an eleven-character string like this:
Dim SSN As String * 11
My question is: does this create any type of performance benefit that would either make the code run faster or perhaps use less memory? Also, would a fixed-length string be allocated in memory differently (i.e.: on the stack as opposed to in the heap)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有性能优势。
但是即使有,除非您在循环中调用多次(例如数百万次),否则任何性能优势都可以忽略不计。
此外,如果不使用整个长度(除非非常短的固定长度字符串),固定长度字符串比可变长度字符串占用更多内存。
与往常一样,在使代码变得更难维护之前,您应该仔细进行基准测试。
与某些 COM API 交互或针对域约束进行建模时(例如您给出的 SSN 示例),通常会看到固定长度字符串
No, there is no performance benefit.
BUT even if there were, unless you were calling many (say millions) times in a loop, any performance benefit would be negligible.
Also, fixed-length strings occupy more memory than variable-length ones if you are not using the entire length (unless very short fixed length strings).
As always, you should carefully benchmark before making the code harder to maintain.
Fixed length strings were usually seen when interacting with some COM API's, or when modelling to domain constraints (such as the example you gave of a SSN)
在 VB6 或更早版本中,我唯一一次必须使用固定长度字符串是在处理 API 调用时。当长度比预期长时,不传递固定长度的字符串有时会导致无法解释的错误,甚至有时比预期短时也会导致无法解释的错误。
如果您正在经历并计划在应用程序中更改它,请确保没有将字符串传递到 API 或外部 DLL,并且程序不需要输出固定长度字段,例如许多 AS/400导入程序。
我个人从未看到过性能差异,因为我正在运行超过 300k 条记录的循环,但当我这样做时,别无选择,只能提供并使用固定长度。然而,VB 喜欢默认使用未定义的长度,所以我想固定长度的性能会较低。
尝试编写一个测试应用程序来执行两个字符串的基本连接,并让它在函数上循环 50k 次。计算两者之间的差异,其中一个长度未定义,另一个长度固定。
The only time in VB6 or earlier that I had to use fixed length strings was with working with API calls. Not passing a fixed length string would cause unexplained errors at times when the length was longer than expected, and even sometimes when shorter than expected.
If you are going through and planning to change that in the application make sure there is no passing of the strings to an API or external DLL, and that the program does not require fixed length fields to be output, such as with many AS/400 import programs.
I personally never got to see a performance difference as I was running loops of 300k+ records, but had no choice but to provide and work with fixed lengths when I did. However VB likes to use undefined lengths by default so I would imagine the performance would be lower for fixed length.
Try writing a test app to perform a basic concatenation of two strings, and have it loop over the function like 50k times. Time the difference between the two of having one undefined length and the other fixed.