In VB 2008, why manipulating shorts takes up more integers?

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 if I change UShort to Short.

Alternatively, 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 sequentially 660 ms, compared to 950 ms for the UInt32 loop.

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

+3
source share
1 answer

, 32 , 16- , .

64- , Int64, Int32...

, .NET ( 32-) int, , short, . uint.

+12

Source: https://habr.com/ru/post/1727002/


All Articles