Profile profile profile. Here is a A-B comparison using mono:
public static IEnumerable<int> UsingYield()
{
yield return 42;
}
public static IEnumerable<int> ReturningArray()
{
return new []{ 42 };
}
(Compiled with -optimize+
enabled)
The yield version instantiates a class that implements IEnumerable
and the whole shebang:
Note
I left out the 163 lines of CIL code implementing the enumerator block 'anonymous' type Program/'<UsingYield>c__Iterator0'
. See it all here: https://gist.github.com/1384014
.method public static hidebysig
default class [mscorlib]System.Collections.Generic.IEnumerable`1<int32> UsingYield () cil managed
{
.custom instance void class [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::'.ctor'() = (01 00 00 00 ) // ....
// Method begins at RVA 0x20f4
// Code size 16 (0x10)
.maxstack 3
.locals init (
class Program/'<UsingYield>c__Iterator0' V_0)
IL_0000: newobj instance void class Program/'<UsingYield>c__Iterator0'::'.ctor'()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: dup
IL_0008: ldc.i4.s 0xfffffffe
IL_000a: stfld int32 Program/'<UsingYield>c__Iterator0'::$PC
IL_000f: ret
} // end of method Program::UsingYield
The array version seems much simpler:
.method public static hidebysig
default class [mscorlib]System.Collections.Generic.IEnumerable`1<int32> ReturningArray () cil managed
{
// Method begins at RVA 0x2110
// Code size 12 (0xc)
.maxstack 8
IL_0000: ldc.i4.1
IL_0001: newarr [mscorlib]System.Int32
IL_0006: dup
IL_0007: ldc.i4.0
IL_0008: ldc.i4.s 0x2a
IL_000a: stelem.i4
IL_000b: ret
} // end of method Program::ReturningArray
On the actual runtime performance, PROFILE PROFILE PROFILE!
return
? – Decemberreturn Enumerable.Repeat(reference, 1);
though this boils down to ayield return
under the covers... – Gymnasiarchyield
: creating a generated, unending IEnumerable (say, a Fibonnaci sequence generator). You have to be careful not to try to loop through the whole IEnumerable if you are going to do that. But, anecdotally, I can tell you that I avoidyield return
whenever it is sensible to do so for performance reasons. It seems that others are confirming this practice. – Crary