在 VB 2008 中,为什么处理短整型比处理整数花费更长的时间?

发布于 2024-08-16 04:01:33 字数 1260 浏览 8 评论 0原文

在此示例中:

Sub Button1_Click(sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim stopwatch1, stopwatch2 As New Stopwatch : Dim EndLoop As ULong = 10000

    stopwatch1.Start()
    For cnt As ULong = 1 To EndLoop
        Dim Number1 As UInt32
        For Number1 = 1 To 20000
            Dim Number2 As UInt32 = 0
            Number2 += 1
        Next
    Next
    stopwatch1.Stop()

    stopwatch2.Start()
    For cnt As ULong = 1 To EndLoop
        Dim Number1 As UShort
        For Number1 = 1 To 20000
            Dim Number2 As UShort = 0
            Number2 += 1
        Next
    Next
    stopwatch2.Stop()

    Label1.Text = "UInt32: " & stopwatch1.ElapsedMilliseconds
    Label2.Text = "UShort: " & stopwatch2.ElapsedMilliseconds
End Sub

对于 UInt32 循环,我始终获得约 950 毫秒的时间;对于 UShort 循环,我始终获得约 1900 毫秒的时间。如果我将 UShort 更改为 Short,我也会得到大约 1900 毫秒。

此外,我可以将第二个循环更改为:

stopwatch2.Start()
For cnt As ULong = 1 To EndLoop
    Dim Number1 As Integer
    For Number1 = 1 To 20000
        Dim Number2 As Integer = 0
        Number2 += 1
    Next
Next
stopwatch2.Stop()

整数循环将始终为 660 毫秒,而 UInt32 循环为 950 毫秒。

与 Short、UShort 和 UInt32 相比,整数是使用速度更快的数据类型吗?如果是这样,为什么?

In this example:

Sub Button1_Click(sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim stopwatch1, stopwatch2 As New Stopwatch : Dim EndLoop As ULong = 10000

    stopwatch1.Start()
    For cnt As ULong = 1 To EndLoop
        Dim Number1 As UInt32
        For Number1 = 1 To 20000
            Dim Number2 As UInt32 = 0
            Number2 += 1
        Next
    Next
    stopwatch1.Stop()

    stopwatch2.Start()
    For cnt As ULong = 1 To EndLoop
        Dim Number1 As UShort
        For Number1 = 1 To 20000
            Dim Number2 As UShort = 0
            Number2 += 1
        Next
    Next
    stopwatch2.Stop()

    Label1.Text = "UInt32: " & stopwatch1.ElapsedMilliseconds
    Label2.Text = "UShort: " & stopwatch2.ElapsedMilliseconds
End Sub

I consistently get about 950 ms for the UInt32 loop, and about 1900 ms for the UShort loop. I get about 1900 ms as well if I changed the UShort to a Short.

Additionally, I can change the second loop to:

stopwatch2.Start()
For cnt As ULong = 1 To EndLoop
    Dim Number1 As Integer
    For Number1 = 1 To 20000
        Dim Number2 As Integer = 0
        Number2 += 1
    Next
Next
stopwatch2.Stop()

And the integer loop will be consistently 660 ms, compared to 950 ms for the UInt32 loop.

Are Integers the faster data type to use compared to Short, UShort and UInt32? If so, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

请叫√我孤独 2024-08-23 04:01:33

我敢打赌这是因为你的机器上的自然字大小是 32 位,而执行 16 位操作实际上会给系统带来更大的压力来剪切和屏蔽这些位。

如果您在 64 位处理器上进行测试,则 Int64 可能会比 Int32 获得更好的结果...

此外,在 .NET 中,所有整数(最多 32 位)算术都会自动向上转换为 int,因此,当您将结果分配回 short 变量时,会导致额外的转换步骤。 uint 也是如此。

I would bet it's because the natural word size on your machine is 32 bit, and doing 16 bit operations actually puts more strain on the system to cut and mask the bits.

If you'd test on a 64 bit processor, you might get better results for Int64 than for Int32...

Also, in .NET all integer (up to 32bit) arithmetics is automatically upcasted to int, so when you're assigning the result back to a short variable, you're causing an extra casting step. Same goes for uint.

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