views:

1957

answers:

4

If you have a Bitmap object that needs to be passed to numerous methods (about 10), and finally to an event where it shall finally be disposed of after its used, would it be (performance wise) beneficial to pass it to every one of those methods by reference instead of value? Passing by value the object is copied, passing by reference its not.

(Performance is critical in this situation, app needs to run as fast as possible)

+4  A: 

Which type are you using exactly for holding the Bitmap? e.g. System.Drawing.Bitmap is a reference type/class, when you pass a reference to a method (as an argument), the reference is passed by value. (a copy of the reference is made... not the object) So 4Bytes would be allocated on a 32 bit machine to hold the copy.

Using the ref keyword has not much bearing on performance except that the same reference is passed (a copy of the reference is not made). It has the following benefits

  • only clears intent that the method taking the parameter may modify it and the caller may get a modified value post execution.
  • and the variable must be initialized by the callee before being passed as an argument to the called function taking the ref parameter
Gishu
Thanks for the extra details bud!
David Anderson
"Using the ref keyword ... only clears intent". Ummm, I don't think that's true. A reference passed-byref (object**) may be reasigned by the caller; affecting which object the callers reference points to. Passing a reference by-value (object*) passes a copy of the reference, isolating the callers-reference from changes within the calle. This article http://preview.tinyurl.com/oda4jz has an example in C#.
corlettk
Correct. Hence the method taking a ref parameter forces the callers to explicitly write ref before the argument - to make sure that the callers understand and agree to the semantics. It clarifies the intent that the caller is passing an argument fully aware that the argument may have a different value/state post the method call.
Gishu
+11  A: 

Bitmap is a reference type. Passing a reference type by value does not copy the object, merely the reference to the object. There would be no performance benefit to passing the Bitmap by reference instead of by value.

+6  A: 

Since Bitmap is a reference type, there is no practical difference for performance in this scenario as it is already being passed by reference to the method.

I'd recommend Jon Skeet's article on the subject for a thorough explanation of how "by reference" and "by value" work in C#.

Jeromy Irvine
Thanks for the link to the article, really helped me. Appreciate it bud
David Anderson
+1  A: 

The 'ref' doesn't pass the object itself into the function, instead it passes a reference to the variable it is stored in/at.

If the object is a class or an interface, then whenever you access that variable in the function, it has to dereference it, and then access the variable. If it was passed in without the 'ref' keyword, then it wouldn't have to do the dereference step (thus it will be slightly faster).

If the object is a struct (or enum or other base type), the 'ref' keyword passes a reference to the variable that stores the struct, which still causes the SLIGHT speed hit of dereferencing whenever you use it, but if you don't specify it, then the program has to allocate memory for a new instance of the struct and then copy it. In most cases, it is faster to pass structs via the ref keyword, but this may not be the case if it is a really small struct, or if its dereferenced a lot (and i mean a LOT).

So if you are passing a struct, then ref is usually the way to go, otherwise there won't really be much of a difference (the overhead in the dereferencing phase is minuscule).

Oh, and to actually answer the question, a Bitmap is a class, so the 'ref' keyword won't really make a speed difference over a non 'ref' parameter.

Grant Peters