Performance of nested yield in a tree

You can improve performance if you unroll recurse to stack, so you will have only one iterator: public IEnumerable<Foo> GetAll() { Stack<Foo> FooStack = new Stack<Foo>(); FooStack.Push(this); while (FooStack.Count > 0) { Foo Result = FooStack.Pop(); yield return Result; foreach (Foo NextFoo in Result.MyChildren) FooStack.Push(NextFoo); } }

yield statement implementation

yield works by building a state machine internally. It stores the current state of the routine when it exits and resumes from that state next time. You can use Reflector to see how it’s implemented by the compiler. yield break is used when you want to stop returning results. If you don’t have a yield … Read more

What are the main uses of yield(), and how does it differ from join() and interrupt()?

Source: http://www.javamex.com/tutorials/threads/yield.shtml Windows In the Hotspot implementation, the way that Thread.yield() works has changed between Java 5 and Java 6. In Java 5, Thread.yield() calls the Windows API call Sleep(0). This has the special effect of clearing the current thread’s quantum and putting it to the end of the queue for its priority level. In … Read more

Return and yield in the same function

Yes, it’ still a generator. The return is (almost) equivalent to raising StopIteration. PEP 255 spells it out: Specification: Return A generator function can also contain return statements of the form: “return” Note that an expression_list is not allowed on return statements in the body of a generator (although, of course, they may appear in … Read more