Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Byval and ByRef arguments should be used based on requirements and knowledge of how they work <em>not</em> on speed.</p> <p><a href="http://www.developer.com/net/vb/article.php/3669066" rel="noreferrer">http://www.developer.com/net/vb/article.php/3669066</a></p> <p><em>In response to a comment by Slough -</em></p> <p><strong>Which consumes more resources at runtime?</strong></p> <p>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&nbsp;MB. This should give you an idea of how few resources are consumed by parameter passing.</p> <p>You may find this series of articles interesting:</p> <p><em><a href="http://www.atalasoft.com/cs/blogs/rickm/archive/2008/04/15/improving-performance-through-stack-allocation-net-memory-management-part-2.aspx" rel="noreferrer">Improving Performance Through Stack Allocation (.NET Memory Management: Part 2)</a></em> </p> <p><strong>Which is faster? ByVal or ByRef.</strong></p> <p>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:</p> <ul> <li>Reference Type - Passed ByRef: 420 ms</li> <li>Reference Type - Passed ByVal: 382 ms</li> <li>Value Type - Passed ByRef: 421 ms</li> <li>Value Type - Passed ByVal: 416 ms</li> </ul> <pre><code>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 " &amp; t.ElapsedMilliseconds) t.Reset() t.Start() For i As Integer = 0 To 100000000 Method2(s) Next t.Stop() Console.WriteLine("Reference Type - ByVal " &amp; t.ElapsedMilliseconds) t.Reset() t.Start() For i As Integer = 0 To 100000000 Method3(i) Next t.Stop() Console.WriteLine("Value Type - ByRef " &amp; t.ElapsedMilliseconds) t.Reset() t.Start() For i As Integer = 0 To 100000000 Method4(i) Next t.Stop() Console.WriteLine("Value Type - ByVal " &amp; t.ElapsedMilliseconds) Console.ReadKey() End Sub </code></pre> <p>Commenting out the variable and assignment in each method -</p> <ul> <li>Reference Type - Passed ByRef: 389 ms</li> <li>Reference Type - Passed ByVal: 349 ms</li> <li>Value Type - Passed ByRef: 416 ms</li> <li>Value Type - Passed ByVal: 385 ms</li> </ul> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload