Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 301
Robin Rodricks Avatar asked Jan 02 '09 21:01

Robin Rodricks


People also ask

What is the difference between ByRef and ByVal?

ByRef = You give your friend your term paper (the original) he marks it up and can return it to you. ByVal = You give him a copy of the term paper and he give you back his changes but you have to put them back in your original yourself.

What is the meaning of ByVal or ByRef in VB net?

The difference is that with ByVal you get a copy of the reference — you have new variable. With ByRef , it's the same variable.


2 Answers

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:

  • Reference Type - Passed ByRef: 420 ms
  • Reference Type - Passed ByVal: 382 ms
  • Value Type - Passed ByRef: 421 ms
  • Value Type - Passed ByVal: 416 ms
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 -

  • Reference Type - Passed ByRef: 389 ms
  • Reference Type - Passed ByVal: 349 ms
  • Value Type - Passed ByRef: 416 ms
  • Value Type - Passed ByVal: 385 ms

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.

like image 97
user50612 Avatar answered Sep 22 '22 03:09

user50612


If you're using a very large value type (Guid is pretty big, for example) it may be very slightly faster to pass a parameter by reference. In other cases, there may be more copying etc when you pass by reference than by value - for instance, if you've got a byte parameter, then one byte is clearly less than the four or eight bytes that the pointer would take if you passed it by reference.

In practice, you should almost never worry about this. Write the most readable code possible, which almost always means passing parameters by value instead of reference. I use ByRef very rarely.

If you want to improve performance and think that ByRef will help you, please benchmark it carefully (in your exact situation) before committing to it.

EDIT: I note in the comments to another (previously accepted, now deleted) answer that there's a great deal of misunderstanding about what ByRef vs ByVal means when it comes to value types. I have an article about parameter passing which has proved popular over the years - it's in C# terminology, but the same concepts apply to VB.NET.

like image 34
Jon Skeet Avatar answered Sep 23 '22 03:09

Jon Skeet