Using ILSpy, listIter looks like this:
public static void listIter(int n) { ListModule.Iterate<int>( new listIter@17 (n), SeqModule.ToList<int>( Operators.CreateSequence<int>( Operators.OperatorIntrinsics.RangeInt32(1, 1, 10000000) ) ) ); }
Here are the main steps:
RangeInt32 creates IEnumerable (which is inexplicably wrapped by CreateSequence )SeqModule.ToList builds a list from this sequencelistIter@17 instance (your lambda) updatedListModule.Iterate moves the list calling lambda for each item
vs forLoop , which is not much different from what you wrote:
public static void forLoop(int n) { for (int x = 1; x < 10000001; x++) { int num = x + n; double num2 = Math.Pow((double)num, 0.1); } }
... no IEnumerable , lambda (it is automatically built-in) or list creation. There is a potentially significant difference in the amount of work performed.
EDIT
For curiosity, here are the FSI time slots for list , seq and for versions:
listIter - Real: 00: 00: 03.889, CPU: 00: 00: 04.680, GC gen0: 57, gen1: 51, gen2: 6
seqIter - Real: 00: 00: 01.340, CPU: 00: 00: 01.341, GC gen0: 0, gen1: 0, gen2: 0
forLoop - Real: 00: 00: 00.565, CPU: 00: 00: 00.561, GC gen0: 0, gen1: 0, gen2: 0
and seq for reference:
let seqIter n = {1..length} |> Seq.iter (fun x -> doSomething (x+n))
Daniel
source share