ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Performance Impact: By Reference or By Value (https://www.excelbanter.com/excel-programming/326168-performance-impact-reference-value.html)

TheVisionThing

Performance Impact: By Reference or By Value
 
Assuming that you're not modifying a parameter within a procedure, is there
any performance difference in passing the parameter as 'ByRef' or 'ByVal'.

For example

Dim arr as variant
arr = Array(12,40)
arr = TimesTwo(arr)

Function TimesTwo(ByRef or ByVal arr as variant)
Dim i as integer,arrTemp as variant
Redim arrTemp(ubound(arr))
For i = lbound(arr) to ubound(arr)
arrTemp(i) = 2 * arr(i)
Next
TimesTwo = arrTemp
End Function

Also can arrays only be declared as variants in situations such as this one.

Thanks,
Wayne C.




Tom Ogilvy

Performance Impact: By Reference or By Value
 
You can only assign an array to a variant or (exl2000 and later) to an
dynamic variant array.

I would think passing byval would require copying the array and this would
incur a penalty.

--
Regards,
Tom Ogilvy

"TheVisionThing" wrote in message
m...
Assuming that you're not modifying a parameter within a procedure, is

there
any performance difference in passing the parameter as 'ByRef' or 'ByVal'.

For example

Dim arr as variant
arr = Array(12,40)
arr = TimesTwo(arr)

Function TimesTwo(ByRef or ByVal arr as variant)
Dim i as integer,arrTemp as variant
Redim arrTemp(ubound(arr))
For i = lbound(arr) to ubound(arr)
arrTemp(i) = 2 * arr(i)
Next
TimesTwo = arrTemp
End Function

Also can arrays only be declared as variants in situations such as this

one.

Thanks,
Wayne C.






TheVisionThing

Performance Impact: By Reference or By Value
 
Interesting info, Tom, thanks.

By dynamic variant array in Excel 2000 or later, did you mean:

Dim arr as variant
Redim arr(2,2)
Redim Preserve arr(2,50)

Where only the last dimension of the array can be altered.

Regards,
Wayne C.

"Tom Ogilvy" wrote in message
...
You can only assign an array to a variant or (exl2000 and later) to an
dynamic variant array.

I would think passing byval would require copying the array and this would
incur a penalty.

--
Regards,
Tom Ogilvy

"TheVisionThing" wrote in message
m...
Assuming that you're not modifying a parameter within a procedure, is

there
any performance difference in passing the parameter as 'ByRef' or
'ByVal'.

For example

Dim arr as variant
arr = Array(12,40)
arr = TimesTwo(arr)

Function TimesTwo(ByRef or ByVal arr as variant)
Dim i as integer,arrTemp as variant
Redim arrTemp(ubound(arr))
For i = lbound(arr) to ubound(arr)
arrTemp(i) = 2 * arr(i)
Next
TimesTwo = arrTemp
End Function

Also can arrays only be declared as variants in situations such as this

one.

Thanks,
Wayne C.








Tom Ogilvy

Performance Impact: By Reference or By Value
 
No,
Dim arr() as Variant

as opposed to

Dim arr as Variant

However, there is no advantage to doing that and it just makes it
incompatible with xl97.

--
Regards,
Tom Ogilvy

"TheVisionThing" wrote in message
m...
Interesting info, Tom, thanks.

By dynamic variant array in Excel 2000 or later, did you mean:

Dim arr as variant
Redim arr(2,2)
Redim Preserve arr(2,50)

Where only the last dimension of the array can be altered.

Regards,
Wayne C.

"Tom Ogilvy" wrote in message
...
You can only assign an array to a variant or (exl2000 and later) to an
dynamic variant array.

I would think passing byval would require copying the array and this

would
incur a penalty.

--
Regards,
Tom Ogilvy

"TheVisionThing" wrote in message
m...
Assuming that you're not modifying a parameter within a procedure, is

there
any performance difference in passing the parameter as 'ByRef' or
'ByVal'.

For example

Dim arr as variant
arr = Array(12,40)
arr = TimesTwo(arr)

Function TimesTwo(ByRef or ByVal arr as variant)
Dim i as integer,arrTemp as variant
Redim arrTemp(ubound(arr))
For i = lbound(arr) to ubound(arr)
arrTemp(i) = 2 * arr(i)
Next
TimesTwo = arrTemp
End Function

Also can arrays only be declared as variants in situations such as this

one.

Thanks,
Wayne C.










TheVisionThing

Performance Impact: By Reference or By Value
 
Understood. Thanks.

Regards,
Wayne C.

"Tom Ogilvy" wrote in message
...
No,
Dim arr() as Variant

as opposed to

Dim arr as Variant

However, there is no advantage to doing that and it just makes it
incompatible with xl97.

--
Regards,
Tom Ogilvy

"TheVisionThing" wrote in message
m...
Interesting info, Tom, thanks.

By dynamic variant array in Excel 2000 or later, did you mean:

Dim arr as variant
Redim arr(2,2)
Redim Preserve arr(2,50)

Where only the last dimension of the array can be altered.

Regards,
Wayne C.

"Tom Ogilvy" wrote in message
...
You can only assign an array to a variant or (exl2000 and later) to an
dynamic variant array.

I would think passing byval would require copying the array and this

would
incur a penalty.

--
Regards,
Tom Ogilvy

"TheVisionThing" wrote in message
m...
Assuming that you're not modifying a parameter within a procedure, is
there
any performance difference in passing the parameter as 'ByRef' or
'ByVal'.

For example

Dim arr as variant
arr = Array(12,40)
arr = TimesTwo(arr)

Function TimesTwo(ByRef or ByVal arr as variant)
Dim i as integer,arrTemp as variant
Redim arrTemp(ubound(arr))
For i = lbound(arr) to ubound(arr)
arrTemp(i) = 2 * arr(i)
Next
TimesTwo = arrTemp
End Function

Also can arrays only be declared as variants in situations such as
this
one.

Thanks,
Wayne C.













All times are GMT +1. The time now is 08:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com