Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2008 在VB 2008中,为什么短字符的操作时间比整数长?_Visual Studio 2008_Optimization_Unsigned_Short - Fatal编程技术网

Visual studio 2008 在VB 2008中,为什么短字符的操作时间比整数长?

Visual studio 2008 在VB 2008中,为什么短字符的操作时间比整数长?,visual-studio-2008,optimization,unsigned,short,Visual Studio 2008,Optimization,Unsigned,Short,在本例中: 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 UI

在本例中:

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换成短的,我也会得到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相比,整数是更快的数据类型吗?如果是这样,原因是什么?

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

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


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

我认为你是对的。我在Win7 64位上运行四核。我将程序更改为针对x86编译,它在大约250毫秒内运行Int32循环。我在Int64中创建了第二个循环,它在x86和x64上以大约650毫秒的速度编译。从x86更改为x64之间的唯一区别是,与x86上的250ms相比,Int32操作在660ms左右需要更长的时间。我知道我认为你是对的,因为我打赌它在x86到Int16上做的事情与x64到Int32上做的事情是一样的。很高兴我提供了一些见解,如果你检查一下,你可能会发现我的最后一段也是对的:)哈哈,我今天在代表处,尽可能多地向上投票:)试着等到第一个厕所之后再实际创建第二个秒表