Guess ... how long will this program take to create the very first output when I == 0? It should be instant, right? And through a lazy yield
estimate after that, he should produce the products in quick succession, right?
static void Main(string[] args) { Stopwatch stopwatch = Stopwatch.StartNew(); int i = 0; foreach (var item in massiveYieldStatement()) { if (i++ % 10000 == 0) Console.WriteLine(stopwatch.ElapsedMilliseconds / 1000); } Console.ReadKey(); } static IEnumerable<string> massiveYieldStatement() { yield return "a"; yield return "a"; .. repeat 200,000 times !! yield return "a"; }
But this is not so! He sits there without exiting for 4 to 21 minutes, and then quickly ends - up to 60 ms in one case! During these minutes, 100% of one main CPU is used, and memory usage is increased. In the actual scenario where I came across this, a Stackoverflow
exception is Stackoverflow
before the first iteration happens! I tried it in debug mode in Visual Studio and in release mode from the command line. I tried this on Windows 7 x64 and Windows Server 2008 R2 x64.
Can anyone explain what is going on here? Does it play for you?
NOTE. This is not real code: real code has much less profitability operators, but much more complicated. This is simply the easiest option.
source share