"By Reference" vs "By Value" question:

bignateyk

Lifer
Apr 22, 2002
11,288
7
0
Here is my declaration in VB right now:

Public Declare Sub find_newXY Lib "vbdll.dll" (matrix As Double, ByVal xVal As Long, ByVal yVal As Long, ByRef finalX As Long, ByRef finalY As Long)


Here is the corresponding declaration in my C++ DLL file:

void _stdcall find_newXY(double *matrix, long xVal, long yVal, long *finalX, long *finalY)

___________________________________________________________________________________________

My question is, even though I have no intention of modifying xVal and yVal within the c++ function, wouldnt it be faster to pass those by reference as well?

This function will be called several times a second so any speed increase would help.

Also, if I did pass them by reference, could I make the function declaration like this:

void _stdcall find_newXY(double *matrix, long &xVal, long &yVal, long *finalX, long *finalY)

or do I need to use "*" instead of "&"
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
The difference between passing two 32bit pointers or 2 64 bit longs is going to miniscule. Actually, by the time they're compiled to machine code, I don't even think you can say for sure which would be faster.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
ByRef is _only_ faster if you are passing large structures that would take time to make copies of to pass ByVal.

A Long is no bigger than the pointer that ends up needing to be dereferenced when you pass it ByRef, so ByVal in that case is actually faster.

If speed really is an issue, don't use VB. Especially for a program dealing with arrays (matrices). Each time you set or get an element of the array you have the overhead of calls to MSVBVM60.DLL, which in turn wraps a call to OLEAUT32.DLL, which in addition to the overhead of calculating the address of the element you are looking for not being optimized because these functions need to handle variable size elements & don't know you are looping, etc, grabs and releases a lock on the safearray for each element you set/get rather than calling SafeArrayLock/SafeArrayUnLock once in C and doing everything you're gonna do with the array in-between the lock/unlock (actually since you realistically know noone else is going to be accessing the array you don't really need to lock it...), or better yet, just using a double ** rather than a safearray.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
gluggglug is right, but with all the win32 api in VB-related questions you have been asking, you should seriously consider purchasing this book.