C# ref keyword
Hello.
I'm writing a small game engine in C# and OpenGL (Tao).
I have a question abot thw ref keyword.
what I would like to determine is if it's more optimized to take parameters by ref, and when it's apropriate.
lets take a vector for example.
public static void Add( ref Vector3 V1, ref Vector3 V2, out Result )
{
Result.X = V1.X + V2.X;
Result.Y = V1.Y + V2.Y;
Result.Z = V1.Z + V2.Z;
}
VS
public static void Add( Vector3 V1, Vector3 V2 )
{
return new Vector3( V1.X + V2.X,
V1.Y + V2.Y,
V1.Z + V2.Z);
}
I'm fairly new to using C#, but using ref is the same as saying go to the place in memory where V1 and V2 is stored and read their values, rather then alternative two, where the in parameters are copies of the vectors you sent to the function. Making copies are slower than reading directly??
Is it more or less optimized to use the function that take ref??
taking ref as in parmeter has other disadvantages, for example you can't pass properties as ref, you can't pass this as ref either.
In general passing large value types by reference will be more efficient than by value. Part of this is due to the fact that the JIT cannot inline functions that take or return value type parameters (fixed in .Net 3.5 SP1). Also the copying can induce significant overhead.
Avoid using properties in simplistic types, such as vector and matrix, because it can introduce hidden performance costs (SlimDX found this out the hard way :P).
Avoid using properties in simplistic types, such as vector and matrix, because it can introduce hidden performance costs (SlimDX found this out the hard way :P).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement