Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static IEnumerable<String> MyYieldCollection {
- get
- {
- wrapper.RunCommand("Fetch First From Water_Mains");
- for (int row = 0; row < tabinfo.GetNumberOfRows() ; row++) //GetNumberOfRows
- //will return 1000+ most of the time.
- {
- yield return wrapper.Evaluate("Water_Mains.col1");
- wrapper.RunCommand("Fetch Next From Water_Mains");
- }
- }
- }
- public static List<String> MyListCollection
- {
- get
- {
- List<String> innerlist = new List<String>();
- wrapper.RunCommand("Fetch First From Water_Mains");
- for (int row = 0; row < tabinfo.GetNumberOfRows(); row++)
- {
- innerlist.Add(wrapper.Evaluate("Water_Mains.col1"));
- wrapper.RunCommand("Fetch Next From Water_Mains");
- }
- return innerlist;
- }
- }
- foreach (var item in MyYieldCollection) //Same thing for MyListCollection.
- {
- Console.WriteLine(item);
- }
- Yield: 2000 (5000000)
- List: 2100 (5000000)
- static void Main()
- {
- GC.Collect(GC.MaxGeneration,GCCollectionMode.Forced);
- int count1 = 0;
- var watch1 = Stopwatch.StartNew();
- for(int i = 0 ; i < 5000 ; i++) {
- foreach (var row in MyYieldCollection)
- {
- count1++;
- }
- }
- watch1.Stop();
- GC.Collect(GC.MaxGeneration,GCCollectionMode.Forced);
- int count2 = 0;
- var watch2 = Stopwatch.StartNew();
- for (int i = 0; i < 5000; i++)
- {
- foreach (var row in MyListCollection)
- {
- count2++;
- }
- }
- watch1.Stop();
- Console.WriteLine("Yield: {0} ({1})", watch1.ElapsedMilliseconds, count1);
- Console.WriteLine("List: {0} ({1})", watch2.ElapsedMilliseconds, count2);
- }
- int rows = tabinfo.Rows.Count;
- for (int row = 0; row < rows; row++) {...}
- public IEnumerable<int> YieldInts()
- {
- for (int i = 0; i < 1000; i++)
- {
- Thread.Sleep(1000) // or do some other work
- yield return i;
- }
- }
- public void Main()
- {
- foreach(int i in YieldInts())
- {
- Console.WriteLine(i);
- if(i == 42)
- {
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment