473,385 Members | 1,764 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,385 software developers and data experts.

Function Declaration

How do you define whether the return value of a function is ByRef or ByVal?

I have a utility class that cleans imported records by doing *really heavy*
string manipulation in lots of different methods, and it operates on
DataTables in excess of 100,000 rows of anywhere between 4 and 30 columns.

I'd like to get the functions to return ByRef for greater speed and memory
efficiency, but I can't see how to do that.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #1
14 2541
I would first suggest that you use a StringBuilder, rather than Strings for
heavy string manipulation.

Second, what is your function returning? If it returns a Reference Type,
then you are only getting a copy of the pointer to the reference type, not a
copy of the reference type itself.

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
How do you define whether the return value of a function is ByRef or
ByVal?

I have a utility class that cleans imported records by doing *really
heavy* string manipulation in lots of different methods, and it operates
on DataTables in excess of 100,000 rows of anywhere between 4 and 30
columns.

I'd like to get the functions to return ByRef for greater speed and memory
efficiency, but I can't see how to do that.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"

Nov 21 '05 #2
>I would first suggest that you use a StringBuilder, rather than Strings for
heavy string manipulation.
Actually, most of the stuff uses Regex's. But I do use StringBuilder where
I can. StringBuilder let me do one of our export batches in half the time
it used to take :):):)
Second, what is your function returning? If it returns a Reference Type,
then you are only getting a copy of the pointer to the reference type, not
a copy of the reference type itself.


Pretty much all the method prototypes in this class look like this:

Public Function CleanSomething(ByRef value As String) As String

There is one method that returns a Structure, also.

I just want to make sure I'm returning the return value ByRef for greater
speed / efficiency.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #3

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP12.phx.gbl...
I would first suggest that you use a StringBuilder, rather than Strings
for heavy string manipulation.


Actually, most of the stuff uses Regex's. But I do use StringBuilder
where I can. StringBuilder let me do one of our export batches in half
the time it used to take :):):)
Second, what is your function returning? If it returns a Reference Type,
then you are only getting a copy of the pointer to the reference type,
not a copy of the reference type itself.


Pretty much all the method prototypes in this class look like this:

Public Function CleanSomething(ByRef value As String) As String

There is one method that returns a Structure, also.

I just want to make sure I'm returning the return value ByRef for greater
speed / efficiency.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"


A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.

HTH,

Mythran

Nov 21 '05 #4
> A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.


That's what I was afraid of. Oh well, you win some, you lose some. I was
just hoping to squeak a little more performance out of this.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #5
> A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.


Not necessarily true for situations when a function returns an object
instance.

If string objects are being created in the function and then the string
object variable is returned, a copy of the pointer gets created if the
return value is captured like this:

Dim X As String = function()

In any event, you don't have to worry about copies of the actual data being
created and passed back.
Nov 21 '05 #6

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:eB**************@TK2MSFTNGP15.phx.gbl...
A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.


That's what I was afraid of. Oh well, you win some, you lose some. I was
just hoping to squeak a little more performance out of this.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"


One way to squeak a little more performance is to read/write only what you
need. Instead of loading all data into a data table or data set, you can
load the strings you need into strings, manipulate what you need, write to
the store. If you are using files, read only what you need into a string,
manipulate it, and write it back out. It sounds as though you are loading
from a database, in this case, do you really need to do all of the
manipulation using client code? Just for kicks, and to see if we can speed
this up a lot, post a little of the logic you are using (code) for
manipulating the data. It might be possible to do all this using SQL
Server, which turns out to be a lot faster for manipulating data in the
database (go figure, a database is faster at manipulating data?!? :P )

Mythran

Nov 21 '05 #7
Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething(ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal & ByRef
refer to how parameters are passed, while Reference Type & Value Type refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a variable
holds the actual value.
String is a Reference type, which means that a String variable or parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable that
holds a reference to the actual string object on the heap. In other words a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are dereferencing a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference Type,
| > then you are only getting a copy of the pointer to the reference type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething(ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|
Nov 21 '05 #8
>"If you pass a String ByVal you
passing the reference itself. In other words a reference to an object."
Just a little clarification here, you are actually passing a COPY of the
original reference to the object, not the original reference itself.

Sub One()
Dim y As String = "Test"
Two(y)
End Sub

Sub Two(ByVal x as String)
'The parameter "x" is a copy of the pointer "y" that points to the
String object on the heap.
'There are now two pointers that point to the one String object
'Only the "x" pointer can be used in this procedure, and only the "y"
pointer can be used in the "One" procedure.
End Sub



"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl... Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething(ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal &
ByRef
refer to how parameters are passed, while Reference Type & Value Type
refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a variable
holds the actual value.
String is a Reference type, which means that a String variable or
parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable
that
holds a reference to the actual string object on the heap. In other words
a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are dereferencing
a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the
time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference
Type,
| > then you are only getting a copy of the pointer to the reference type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething(ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for
greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|

Nov 21 '05 #9
If I pass a structure byval that contains both reference and value types,
what exactly am I passing and if I change a structure field in my function,
is the structure field in the calling procedure changed also?
--
Dennis in Houston
"Jay B. Harlow [MVP - Outlook]" wrote:
Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething(ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal & ByRef
refer to how parameters are passed, while Reference Type & Value Type refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a variable
holds the actual value.
String is a Reference type, which means that a String variable or parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable that
holds a reference to the actual string object on the heap. In other words a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are dereferencing a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference Type,
| > then you are only getting a copy of the pointer to the reference type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething(ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|

Nov 21 '05 #10
Structures are Value Types. If you pass a Value Type ByVal, you get a copy
of the structure. Changes to the passed structure do not affect the
original structure.
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
If I pass a structure byval that contains both reference and value types,
what exactly am I passing and if I change a structure field in my
function,
is the structure field in the calling procedure changed also?
--
Dennis in Houston
"Jay B. Harlow [MVP - Outlook]" wrote:
Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething(ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal &
ByRef
refer to how parameters are passed, while Reference Type & Value Type
refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a
variable
holds the actual value.
String is a Reference type, which means that a String variable or
parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable
that
holds a reference to the actual string object on the heap. In other words
a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are
dereferencing a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:uH*************@TK2MSFTNGP12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than
Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the
time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference
Type,
| > then you are only getting a copy of the pointer to the reference
type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething(ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for
greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|

Nov 21 '05 #11
Dennis,
| If I pass a structure byval that contains both reference and value types,
| what exactly am I passing
A structure is a value type, you are receiving a copy of all the values. If
the structure contains a String field, you get a copy of the reference to a
single copy of the String object on the heap.

| if I change a structure field in my function,
| is the structure field in the calling procedure changed also?
You are changing your copy of the structure values, not the original.

Hope this helps
Jay

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
| If I pass a structure byval that contains both reference and value types,
| what exactly am I passing and if I change a structure field in my
function,
| is the structure field in the calling procedure changed also?
| --
| Dennis in Houston
|
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
|
| > Mike,
| > In addition to the other comments.
| >
| > I would define the function as:
| >
| > | Public Function CleanSomething(ByVal value As String) As String
| >
| > To help ensure the "fastest" possible function! Remember that ByVal &
ByRef
| > refer to how parameters are passed, while Reference Type & Value Type
refer
| > to how values are stored.
| >
| > ByVal passes a copy of the variable as the parameter.
| > ByRef passes a reference to the variable as the parameter.
| >
| > Reference Types exist on the heap, a variable holds a reference to the
| > actual object on the heap.
| > Value Types exist on the stack or nested inside another object, a
variable
| > holds the actual value.
| >
| >
| > String is a Reference type, which means that a String variable or
parameter
| > holds a reference to the actual string object on the Heap. If you pass a
| > String ByRef to a routine, you are passing a reference to the variable
that
| > holds a reference to the actual string object on the heap. In other
words a
| > reference to a reference to an object. If you pass a String ByVal you
| > passing the reference itself. In other words a reference to an object.
| >
| > I would expect ByRef String to be slightly slower as you are
dereferencing a
| > reference each time you want to access the String's value.
| >
| > When you define your function "As String" you are receiving a copy of
the
| > reference to the actual string object on the heap. There is only one
| > instance of the String on the heap.
| >
| > Hope this helps
| > Jay
| >
| > "Mike Labosh" <ml*****@hotmail.com> wrote in message
| > news:uH*************@TK2MSFTNGP12.phx.gbl...
| > | >I would first suggest that you use a StringBuilder, rather than
Strings
| > for
| > | >heavy string manipulation.
| > |
| > | Actually, most of the stuff uses Regex's. But I do use StringBuilder
| > where
| > | I can. StringBuilder let me do one of our export batches in half the
time
| > | it used to take :):):)
| > |
| > | > Second, what is your function returning? If it returns a Reference
Type,
| > | > then you are only getting a copy of the pointer to the reference
type,
| > not
| > | > a copy of the reference type itself.
| > |
| > | Pretty much all the method prototypes in this class look like this:
| > |
| > | Public Function CleanSomething(ByRef value As String) As String
| > |
| > | There is one method that returns a Structure, also.
| > |
| > | I just want to make sure I'm returning the return value ByRef for
greater
| > | speed / efficiency.
| > |
| > | --
| > | Peace & happy computing,
| > |
| > | Mike Labosh, MCSD
| > |
| > | "Mr. McKittrick, after very careful consideration, I have
| > | come to the conclusion that this new system SUCKS."
| > | -- General Barringer, "War Games"
| > |
| > |
| >
| >
| >
Nov 21 '05 #12
Jay,

I would expect ByRef String to be slightly slower as you are dereferencing
a
reference each time you want to access the String's value.

Can you give us how many picoseconds on a 2Ghz Intel P4 computer with dual
channels.

:-))

Cor
Nov 21 '05 #13
Cor,
I did say *slightly* slower! ;-)

Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ON*************@TK2MSFTNGP12.phx.gbl...
| Jay,
|
| >
| > I would expect ByRef String to be slightly slower as you are
dereferencing
| > a
| > reference each time you want to access the String's value.
| >
| Can you give us how many picoseconds on a 2Ghz Intel P4 computer with dual
| channels.
|
| :-))
|
| Cor
|
|
Nov 21 '05 #14
> Can you give us how many picoseconds on a 2Ghz Intel P4 computer with dual
channels.


Go ahead and make fun all you want. If I can save a picosecond on each
iteration of this giant loop, running on a 4-CPU server, I will :)

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #15

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

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
20
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
20
by: svata | last post by:
Hello there, after some time of pondering I come to some solution which would suit me best. Please correct, if I am wrong. Function has two parameters. A string array, better said a pointer to...
4
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.