Which is faster? ByVal or ByRef?

后端 未结 7 2000
猫巷女王i
猫巷女王i 2020-12-02 11:29

In VB.NET, which is faster to use for method arguments, ByVal or ByRef?

Also, which consumes more resources at runtime (RAM)?


<
7条回答
  •  生来不讨喜
    2020-12-02 11:54

    My curiosity was to check the different behaviours depending object and memory usages

    The result seems demostrate that ByVal always wins, the resource depends if collect memory or less (4.5.1 only)

    Public Structure rStruct
        Public v1 As Integer
        Public v2 As String
    End Structure
    
    Public Class tClass
        Public v1 As Integer
        Public v2 As String
    End Class
    
    
    
    Public Sub Method1(ByRef s As String)
        Dim c As String = s
    End Sub
    
    Public Sub Method2(ByVal s As String)
        Dim c As String = s
    End Sub
    
    Public Sub Method3(ByRef i As Integer)
        Dim x As Integer = i
    End Sub
    
    Public Sub Method4(ByVal i As Integer)
        Dim x As Integer = i
    End Sub
    
    Public Sub Method5(ByVal st As rStruct)
        Dim x As rStruct = st
    End Sub
    
    Public Sub Method6(ByRef st As rStruct)
        Dim x As rStruct = st
    End Sub
    
    
    Public Sub Method7(ByVal cs As tClass)
        Dim x As tClass = cs
    End Sub
    
    Public Sub Method8(ByRef cs As tClass)
        Dim x As tClass = cs
    End Sub
    Sub DoTest()
    
        Dim s As String = "Hello World!"
        Dim cs As New tClass
        cs.v1 = 1
        cs.v2 = s
        Dim rt As New rStruct
        rt.v1 = 1
        rt.v2 = s
        Dim k As Integer = 5
    
    
    
    
        ListBox1.Items.Add("BEGIN")
    
        Dim t As New Stopwatch
        Dim gt As New Stopwatch
    
        If CheckBox1.Checked Then
            ListBox1.Items.Add("Using Garbage Collection")
            System.Runtime.GCSettings.LargeObjectHeapCompactionMode = System.Runtime.GCLargeObjectHeapCompactionMode.CompactOnce
            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.GetTotalMemory(False)
        End If
    
        Dim d As Double = GC.GetTotalMemory(False)
    
        ListBox1.Items.Add("Free Memory:   " & d)
    
        gt.Start()
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method1(s)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Reference Type - ByRef " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method2(s)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Reference Type - ByVal " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method3(i)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Value Type - ByRef " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method4(i)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Value Type - ByVal " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
    
        For i As Integer = 0 To 100000000
            Method5(rt)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Structure Type - ByVal " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
    
        For i As Integer = 0 To 100000000
            Method6(rt)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Structure Type - ByRef " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
    
        For i As Integer = 0 To 100000000
            Method7(cs)
        Next
        t.Stop()
    
        ListBox1.Items.Add("Class Type - ByVal " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
    
        For i As Integer = 0 To 100000000
            Method8(cs)
        Next
        t.Stop()
        gt.Stop()
    
        ListBox1.Items.Add("Class Type - ByRef " & t.ElapsedMilliseconds)
        ListBox1.Items.Add("Total time " & gt.ElapsedMilliseconds)
        d = GC.GetTotalMemory(True) - d
        ListBox1.Items.Add("Total Memory Heap consuming (bytes)" & d)
    
    
        ListBox1.Items.Add("END")
    
    End Sub
    
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
        DoTest()
    
    End Sub
    

提交回复
热议问题