RecordCount 与 BOF/EOF 评估
我正在维护一组代码,其中包含 SqlDataAdapter 的包装类来加载 System.Data.DataTable。它有一个通用函数来确定数据表是否“有记录”。我知道这是一个小问题,但出于好奇......哪种方法更快?
现有:
Public ReadOnly Property hasRecords() As Boolean
Get
hasRecords = CBool((CBool(BOF = True) And CBool(EOF = True)) = False)
End Get
End Property
或 可能的新内容:
Public ReadOnly Property hasRecords() As Boolean
Get
hasRecords = IIf(RecordCount > 0, True, False)
End Get
End Property
如果 RecordCount 被定义为记录集属性中的固定值,我认为单个 eval 的计数会比它使用的多部分转换/eval BOF/EOF 方法更快。
还有什么不改变的理由吗?
I'm maintaining a set of code that has a wrapper class for SqlDataAdapter to load a System.Data.DataTable. It has a generic function to determine if the DataTable "hasRecords". I know it's a minor issue, but out of curiosity... Which is the faster method to use?
Existing:
Public ReadOnly Property hasRecords() As Boolean
Get
hasRecords = CBool((CBool(BOF = True) And CBool(EOF = True)) = False)
End Get
End Property
or
Posssible new:
Public ReadOnly Property hasRecords() As Boolean
Get
hasRecords = IIf(RecordCount > 0, True, False)
End Get
End Property
If RecordCount is defined as a fixed value in as a recordset property I would think the count would be faster as a single eval vs the multipart conversion/eval BOF/EOF method it is using.
Is there any other reason not to change it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是一种不必要且有潜在风险的优化; RecordCount 可能并不总是可用(请参阅文档< /a>),而 BOF 和 EOF 应始终返回适当的答案。因此,您的优化可能会破坏行为,同时可能仅提供最小的性能回报(如果您真的对此感兴趣,则应该进行测试)。
This seems like an unnecessary and potentially risky optimization; RecordCount may not always be available (see the documentation), while BOF and EOF should always return appropriate answers. So your optimization potentially breaks behavior while likely providing only a minimal return in performance (which if you are really interested in, you should test).
就涉及的算术而言,
与
RecordCount 测试相比,似乎要省力一些。然而,这将是一个天真的结论。您需要自己计时,看看是否有任何差异。我的大胆猜测:在当今的现代 CPU 上——通过流水线、分支预测和其他先进技术——可能不会有可测量的差异。
顺便说一句,虽然您可能会喜欢 RecordCount,但我发现 BOF/EOF 更便携。只是说说而已。
In terms of the arithmetic involved,
versus
it would seem the RecordCount test may be less effort. However, that would be a naive conclusion to reach. You'll need to do the timing yourself to see if there's any difference. My bold guess: On today's modern CPU - thru pipelining, branch prediction, and other advanced techniques - there will probably be no measurable difference.
BTW, Although you'll probably be fine with RecordCount, I've found that BOF/EOF to be more portable. Just sayin'.