Byval Vs ByRef Performance
-
We have a main method which calls a sub method 50 times and in turn the sub method calls one more method 5 times. From the first the datatable is passed by byval and one or two value types are passed by byval. The method doesn't make any changes to the variables 1. Byval or Byref - which will be faster-multiple times and multilevels calling? 2. Will that be a good idea to have multiple levels of methods to be called multiple times. We have lot of local variables. Will the garbage collectors's headache be more in creating and destroying the objects? Thanks
-
We have a main method which calls a sub method 50 times and in turn the sub method calls one more method 5 times. From the first the datatable is passed by byval and one or two value types are passed by byval. The method doesn't make any changes to the variables 1. Byval or Byref - which will be faster-multiple times and multilevels calling? 2. Will that be a good idea to have multiple levels of methods to be called multiple times. We have lot of local variables. Will the garbage collectors's headache be more in creating and destroying the objects? Thanks
ByVal means that the method will create its own copy of the variable, and any changes will not be passed back to the method that called it. ByRef means the method will use the variable you passed in and update it. Some objects (I believe DataTables are one example of this) are always passed ByRef even if you specify ByVal because they are so complex. It sounds like what you want to do is use ByRef. Running a method that updates a couple variables 50 * 5 = 250 times shouldn't be too much for an average computer today. If you were updating several hundreds of thousands than you may worry about it more. Hope this helps.
-
We have a main method which calls a sub method 50 times and in turn the sub method calls one more method 5 times. From the first the datatable is passed by byval and one or two value types are passed by byval. The method doesn't make any changes to the variables 1. Byval or Byref - which will be faster-multiple times and multilevels calling? 2. Will that be a good idea to have multiple levels of methods to be called multiple times. We have lot of local variables. Will the garbage collectors's headache be more in creating and destroying the objects? Thanks
First: abandon any notion of ByRef vs. ByVal from VB6. Second: there is a difference with ByVal and ByRef wrt object instances. What ByRef and ByVal indicate are which memory address to copy to the method's stack. Lets look at the following code:
Private Sub DataTableCreator() Dim dt As New DataTable() ByValReceiver(dt) ByRefReceiver(dt) End Sub Private Sub ByValReceiver(ByVal valdt As DataTable) valdt = Nothing End Sub Private Sub ByRefReceiver(ByRef refdt As DataTable) refdt = Nothing End Sub
ByValReceiver will take in the memory address of the object on the heap and assign it to a local variable "valdt" on it's stack. ByRefReceiver will take in the memory address of the variable "dt" from DataTableCreator's stack and assign that to refdt. So what does this do for you? Two things. 1) If you run the code you'll see ByValReceiver won't effect dt in DataTableCreator, but ByRefReceiver does. When you assign Nothing you're in effect clearing the memory reference from the variable. refdt is really a reference to a reference (or pointer to a pointer in in the old days :) **p), setting it to nothing clears the real reference to the DataTable, which is dt in DataTableCreator. 2) I can't prove this since .Net can be a black box, but I fully expect that nesting calls with ByRef will cause multiple dereferencing steps (going to the memory address in the variable) to get back to the heap. If you used refdt, first it must dereference to dt, then dereference dt to get to the actual DataTable instance on the heap. This would actually marginally slow things down. ByRef is required for when you want value types, which are held on the local stack (i.e. integer, long, structures etc) to be modified by the called method. Think out parameters. Objects should be handed down ByVal, unless you really want to use the trick above! Hope this clears things up at least a little? Coding since '85 - ATARI BASIC, Forth, C, Java, C#, VB.Net .. -
ByVal means that the method will create its own copy of the variable, and any changes will not be passed back to the method that called it. ByRef means the method will use the variable you passed in and update it. Some objects (I believe DataTables are one example of this) are always passed ByRef even if you specify ByVal because they are so complex. It sounds like what you want to do is use ByRef. Running a method that updates a couple variables 50 * 5 = 250 times shouldn't be too much for an average computer today. If you were updating several hundreds of thousands than you may worry about it more. Hope this helps.
-
We have a main method which calls a sub method 50 times and in turn the sub method calls one more method 5 times. From the first the datatable is passed by byval and one or two value types are passed by byval. The method doesn't make any changes to the variables 1. Byval or Byref - which will be faster-multiple times and multilevels calling? 2. Will that be a good idea to have multiple levels of methods to be called multiple times. We have lot of local variables. Will the garbage collectors's headache be more in creating and destroying the objects? Thanks
K.P.Kannan wrote:
The method doesn't make any changes to the variables
Then you should not pass anything by reference.
K.P.Kannan wrote:
Byval or Byref - which will be faster-multiple times and multilevels calling?
ByVal will be faster, and that is the default way of passing parameters. When you pass an object by value, you are passing a copy of the reference to the object, and it will be handled just as afficiently as a local variable in the method. When you pass an object by reference, you are passing a reference to the reference to the object. Whenever you use it in the method, it has to be dereferenced, which adds a small overhead each time. The ByVal and ByRef keywords are mostly kept for compatibility reasons. In object oriented programming they are very rarely used at all.
K.P.Kannan wrote:
Will that be a good idea to have multiple levels of methods to be called multiple times.
Unless you are making a huge number of calls to the methods, it doesn't matter much.
K.P.Kannan wrote:
We have lot of local variables. Will the garbage collectors's headache be more in creating and destroying the objects?
Local variables are allocated on the stack, and don't have to be garbage collected at all. If you have local variables that are references, and create objects for those references, the objects will of course be garbage collected. Creating and freeing objects are considerably cheaper in .NET than it was in VB6. As long as you make use of the objects, you should not normally be concerned with the cost of creating and freeing them. If you create and free the same kind of objects a lot, you might consider a way of reusing them, but on the other hand many objects are not even built to be reused in that way.
--- single minded; short sighted; long gone;
-
We have a main method which calls a sub method 50 times and in turn the sub method calls one more method 5 times. From the first the datatable is passed by byval and one or two value types are passed by byval. The method doesn't make any changes to the variables 1. Byval or Byref - which will be faster-multiple times and multilevels calling? 2. Will that be a good idea to have multiple levels of methods to be called multiple times. We have lot of local variables. Will the garbage collectors's headache be more in creating and destroying the objects? Thanks
Thank you all for the explanations. It was nice and explained a lot