c# - IEnumerable state machine larger than return List -
i'm not sure has huge effect on modern day computers.
however, interesting know.
i've been reading these resources trying find out if on head of using iterator state machine larger returning list. emphasis not on ability lazy load, in example makes no odds on when evaluated.
resoures:
- c# yield return performance
- when not use yield (return)
- is there ever reason not use 'yield return' when returning ienumerable?
- https://blogs.msdn.microsoft.com/wesdyer/2007/03/23/all-about-iterators/
- https://coding.abel.nu/2011/12/return-ienumerable-with-yield-return/
- is 'yield return' slower "old school" return?
now last resource closest.
it comes down if know length of list constant is:
is footprint of ienumerable<t>
state machine >
list<t>
so:
public void ienumerable<customclass> getcustoms(string input) { foreach (var ch in input.tochararray()) yield return new customclass(ch); }
does state machine create larger overhead than:
public void ienumerable<customclass> getcustoms(string input) { var result = new list<customclass>(input.length); foreach(var ch in input.tochararray()) result.add(new customclass(ch)); return result; }
obviously contrived example. illustrates point.
i can imagine, if there lot of preprocessing happening before iteration in methods, state machine contain pre processing overhead.
logic allocation of list
memory far less allocation ienumerable
state machine memory.
obviouosly bigger enumeration, more starts flip other way , ienumerable
state machine becomes smaller foot print.
are these observations correct?
Comments
Post a Comment