473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

ByRef is better of ByValue for value types ?


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

Aug 22 '07 #1
4 1386
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"pamela fluente" <pa***********@libero.itwrote in message
news:11*********************@z24g2000prh.googlegro ups.com...
>
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

Aug 22 '07 #2

"pamela fluente" <pa***********@libero.itwrote in message
news:11*********************@z24g2000prh.googlegro ups.com...
>
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.
>
-P

Aug 22 '07 #3
On 22 Ago, 19:47, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"pamela fluente" <pamelaflue...@libero.itwrote in message

news:11*********************@z24g2000prh.googlegro ups.com...


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.

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


Aug 23 '07 #4
On Aug 23, 9:55 am, pamela fluente <pamelaflue...@libero.itwrote:
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

Aug 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Logico | last post by:
Hi everybody, I've tried to use the byref keyword for passing arguments to subroutines and functions in my ASP pages with VBScript, but it seems that both byref and byval are irrilevant, as simple...
6
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
19
by: Rob Panosh | last post by:
Hello, Ok here is the senerio: ..... Dim myArrayList as New ArrayList(0) me.Test_A( myArrayList )
3
by: tinman | last post by:
Hi.... Assume Function A in an application calls Function GetSomeData in another assembly..... which then is the prefered method to return the SqlDatareader object back to Function A (and why...
6
by: ari | last post by:
hey all, i have the following 2 classes: Public Class DataAccessLayer .... .... Public Sub GetRecords(ByRef ds As DataSet1) ds = New DataSet1
9
by: Samuel Shulman | last post by:
Hi I wander there is a way to return ByRef just like passing ByRef What I want to achieve is the following: Call a method that returns an object Use that call as an argument to a ByRef...
5
by: Rob Meade | last post by:
Hi all, Until just recently most of my functions would have all been using Byval, when I realised the advantages of ByRef, where appropriate I have used it instead, my question - are there any...
9
by: ManicQin | last post by:
Hello all. I've templated a simple class like the next: template <class _T> class base { public: base(_T newVal):m_data(newVal){}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.