RecordCount 与 BOF/EOF 评估

发布于 2024-11-07 08:08:44 字数 644 浏览 0 评论 0原文

我正在维护一组代码,其中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似狗非友 2024-11-14 08:08:44

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).

清音悠歌 2024-11-14 08:08:44

就涉及的算术而言,

hasRecords = Not(BOF AndAlso EOF)

hasRecords = (RecordCount > 0)

RecordCount 测试相比,似乎要省力一些。然而,这将是一个天真的结论。您需要自己计时,看看是否有任何差异。我的大胆猜测:在当今的现代 CPU 上——通过流水线、分支预测和其他先进技术——可能不会有可测量的差异。


顺便说一句,虽然您可能会喜欢 RecordCount,但我发现 BOF/EOF 更便携。只是说说而已。

In terms of the arithmetic involved,

hasRecords = Not(BOF AndAlso EOF)

versus

hasRecords = (RecordCount > 0)

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文