VB6 - 在 VB6 中使用固定宽度的字符串是否有任何性能优势? [英] VB6 - Is there any performance benefit gained by using fixed-width strings in VB6?

查看:26
本文介绍了VB6 - 在 VB6 中使用固定宽度的字符串是否有任何性能优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET Visual Basic 之前的版本中,程序员可以将字符串声明为特定宽度.例如,我知道社会安全号码(在美国)总是 11 个字符.因此,我可以声明一个将社会安全号码存储为 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)?

推荐答案

在 VB6 或更早版本中,我唯一一次必须使用固定长度字符串是处理 API 调用.不传递固定长度的字符串有时会导致无法解释的错误,有时长度比预期长,甚至有时比预期短.

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.

如果您正在经历并计划在应用程序中更改它,请确保没有将字符串传递给 API 或外部 DLL,并且程序不需要输出固定长度的字段,例如许多AS/400 导入程序.

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.

我个人从来没有看到性能差异,因为我正在运行 300k+ 记录的循环,但是当我这样做时,别无选择,只能提供和使用固定长度.然而,VB 喜欢默认使用未定义的长度,所以我认为固定长度的性能会更低.

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.

尝试编写一个测试应用程序来执行两个字符串的基本连接,并让它在函数上循环 50k 次.计算具有一个未定义长度和另一个固定长度的两者之间的差异.

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.

这篇关于VB6 - 在 VB6 中使用固定宽度的字符串是否有任何性能优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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