vb.netperformancebyrefbyval

Which is faster? ByVal or ByRef?


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

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


I read through this question, but the answers are not applicable or specific enough.


Solution

  • Byval and ByRef arguments should be used based on requirements and knowledge of how they work not on speed.

    http://www.developer.com/net/vb/article.php/3669066

    In response to a comment by Slough -

    Which consumes more resources at runtime?

    Parameters are passed on the stack. The stack is very fast, because its memory allocation is simply a pointer increment to reserve a new "frame" or "allocation record." Most .NET parameters do not exceed the size of a machine register so little if any "stack" space is used to pass parameters. In fact basic types and pointers are both allocated on the stack. The stack size in .NET is limited to 1 MB. This should give you an idea of how few resources are consumed by parameter passing.

    You may find this series of articles interesting:

    Improving Performance Through Stack Allocation (.NET Memory Management: Part 2)

    Which is faster? ByVal or ByRef.

    It is difficult at best to measure accurately and fairy - depending on the context of your measurement, but a benchmark I wrote calling a method 100 million times came up with the following:

    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
    
    Sub Main()
    
        Dim s As String = "Hello World!"
        Dim k As Integer = 5
    
        Dim t As New Stopwatch
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method1(s)
        Next
        t.Stop()
    
        Console.WriteLine("Reference Type - ByRef " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method2(s)
        Next
        t.Stop()
    
        Console.WriteLine("Reference Type - ByVal " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method3(i)
        Next
        t.Stop()
    
        Console.WriteLine("Value Type - ByRef " & t.ElapsedMilliseconds)
    
        t.Reset()
        t.Start()
        For i As Integer = 0 To 100000000
            Method4(i)
        Next
        t.Stop()
    
        Console.WriteLine("Value Type - ByVal " & t.ElapsedMilliseconds)
    
        Console.ReadKey()
    
    End Sub
    

    Commenting out the variable and assignment in each method -

    One could conclude that passing reference types (strings, classes) ByVal will save some time. You might also say that passing value types (integer, byte) - ByVal will save some time.

    Again the time is negligible in the grand scheme of things. What's more important is using ByVal and ByRef properly and understanding what's going on "behind the scenes." The algorithms implemented in your routines will most surely affect the runtime of your program many times more.