この例では:
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ミリ秒かかります。
さらに、2番目のループを次のように変更できます。
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()
また、整数ループは、UInt32ループの950ミリ秒と比較して、一貫して660ミリ秒になります。
整数は、Short、UShort、およびUInt32と比較して、使用するデータ型が高速ですか?もしそうなら、なぜですか?