ByRef is better of ByValue for value types ?

  • Thread starter Thread starter pamela fluente
  • Start date Start date
P

pamela fluente

I look for an opinion.

I am wirting a huge number of Rectangles on a graphic object.

I have a lot of places where these rectangles are passed as parameters
of some function.

Provided that these arguments are never changed within the funtions,
I am wondering if it is, in any case, better to pass them ByRef.

My intuitive guess is that it's probably faster to pass the references
alone, instead of copies
of top, left, width, height ...

Does this make sense, or is just nonsense ?

-P
 
Pamela,

There is definitely some overhead in copying the rectangle, and I would
say that over a large number of operations, that it is faster. However, the
definition of "large" is variable, at best.

You are also losing any guarantees you have about how the method will
affect the paramter, and that might not be worth it.

I would do some performance tests, checking to see how much difference
in time there is between passing by ref and by value.

If this question is in reference to passing it in one place, and not in
a tight loop, for example, then I would say pass by value, as you are
optimizing prematurely.
 
pamela fluente said:
I look for an opinion.

I am wirting a huge number of Rectangles on a graphic object.

I have a lot of places where these rectangles are passed as parameters
of some function.

Provided that these arguments are never changed within the funtions,
I am wondering if it is, in any case, better to pass them ByRef.

My intuitive guess is that it's probably faster to pass the references
alone, instead of copies
of top, left, width, height ...

Does this make sense, or is just nonsense ?

Much of the time, the JIT should inline the function, and there won't be a
copy made at all.

You may also see gains by vectorizing the functions that act on rectangles,
so you can pass in an array (or IEnumerable<Rectangle>, but array is faster
if you don't need the flexibility) instead of a single Rectangle. This
improvement would surely dwarf the difference between byval and byref,
especially if you ever need any form of dynamic call (virtual call, or
remote call) which can't be inlined.
 
Much of the time, the JIT should inline the function, and there won't be a
copy made at all.

You may also see gains by vectorizing the functions that act on rectangles,
so you can pass in an array (or IEnumerable<Rectangle>, but array is faster
if you don't need the flexibility) instead of a single Rectangle. This
improvement would surely dwarf the difference between byval and byref,
especially if you ever need any form of dynamic call (virtual call, or
remote call) which can't be inlined.
I have done some testing.

According to them it seems there is a remarkable gain by passing
ByRef.
ByVal on my pc take almost double time. Surprising eh ?

I have also tried by passing a Dictionary (just to try a Byref value)
and could not see
any difference.

I do not understand the sentence "Much of the time, the JIT should
inline the function, and there won't be a
copy made at all. ". How would that work, how can it optimize and
make copies of values that are unknow at compile time ?


For the other suggestion:
You may also see gains by vectorizing the functions that act on rectangles,
so you can pass in an array (or IEnumerable<Rectangle>, but array is faster

I can understand that. But am I not adding a further level of
indirection?
Is this worth and not time consuming ?

-P
 
I have done some testing.

According to them it seems there is a remarkable gain by passing
ByRef.
ByVal on my pc take almost double time. Surprising eh ?

It will entirely depend on the structure being passed.
I have also tried by passing a Dictionary (just to try a Byref value)
and could not see any difference.

That's likely to be *slower* when passed by reference, as there's an
extra layer of redirection involved.
I do not understand the sentence "Much of the time, the JIT should
inline the function, and there won't be a
copy made at all. ". How would that work, how can it optimize and
make copies of values that are unknow at compile time ?

The JIT can determine when a method can be inlined, and do perform
appropriate optimisations (such as not copying a value when the
original can be used instead).

Jon
 
Back
Top