Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ListAndYield()
- {
- const int count = 10000000;
- var sw = new Stopwatch();
- sw.Start();
- var list = ByList(count).ToArray();
- sw.Stop();
- Debug.WriteLine(string.Format(
- "List {1:#,##0} items: {0:#,##0.000} ms", sw.ElapsedMilliseconds, list.Count()));
- sw.Reset();
- sw.Start();
- list = ByYield(count).ToArray();
- sw.Stop();
- Debug.WriteLine(string.Format(
- "Yield {1:#,##0} items: {0:#,##0.000} ms", sw.ElapsedMilliseconds, list.Count()));
- }
- private IEnumerable<int> ByList(int count)
- {
- var ret = new List<int>();
- for(var i = 0; i < count; i++)
- {
- ret.Add(i);
- }
- return ret;
- }
- private IEnumerable<int> ByYield(int count)
- {
- for(var i = 0; i < count; i++)
- {
- yield return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement