Re: textbook authors: passing by ref is NOT more efficient!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I am not sure what textbook author (or which authors) you are talking about,
so I cannot assess the comment in context. I would agree, overall, with the
blanket statement that passing by ref is "more efficient" if we are seeing
efficiency in terms of performance and we are talking reference objects. If
I run a million iterations, I will see a slight performance benefit by
passing by reference, as I am not allocating memory in many cases.

But, I am trading "safety" for "efficiency" here. Take, for example, the
classic example (using int values, as you have mentioned this):

static void Main()
{
int value = 1;

Console.WriteLine("Value before by valroutine: {0}", value);
Console.WriteLine(GetNextNumberVal(value));
Console.WriteLine("Value after by valroutine {0}", value);

Console.WriteLine(String.Empty);
Console.WriteLine("Value before by ref routine: {0}", value);
Console.WriteLine(GetNextNumberRef(ref value));
Console.WriteLine("Value after by ref routine {0}", value);

Console.Read();
}

private static string GetNextNumberRef(ref int input)
{
input++;
return String.Format("The next number is {0}", input);
}

private static string GetNextNumberVal(int input)
{
input++;
return String.Format("The next number is {0}", input);
}

Of course, I have seen people who purposefully use by ref to allow a routine
to accomplish two jobs, which is also very dangerous.

Short version: I agree with your statements, but would have to see the
printed "efficiency" statement to determine whether, in context, it was
valid.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Aaron Watters" <aaron.watters@xxxxxxxxx> wrote in message
news:6dca001b-aae6-4cfa-bfde-48d7a9604ebf@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi. I've been teaching C# and I'm tired of students
telling me that they passed ints by ref when they don't
need to because it's "more efficient". I've seen this myth
repeated in many textbooks, but it's completely wrong
in C# (and it's always wrong for ints in any language).

In the case of C++ if you are passing arrays (if I recall)
it can be pretty important to pass by ref and not by
value because otherwise the whole (3GB) array gets
copied onto the call stack.

However, in the case of C# all arrays
are reference types, so you always actually copy a
reference to the array object even if you don't say "ref",
so there is no imperative to pass by ref unless you want
to change the binding of the variable naming the array.

For all common value types the size of a reference to the
value is about the same size as the size of the value itself,
and you are adding an additional level of indirection
-- so you are not saving anything in space or time by passing
by ref when you don't have to.

The only exception is "structs" which can hypothetically
be large, but they are only used for special purposes
and should be avoided in most vanilla programming.

Please correct me if I'm wrong, or if not, please
*stop* *confusing* *my* *students* who should always
pass by value unless it's absolutely necessary to pass
by ref.

Annoyed: -- Aaron (Stagnant) Watters

===
http://www.xfeedme.com/nucular/gut.py/go?FREETEXT=immoral+english


.



Relevant Pages