Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace test {
- class Program {
- static IEnumerable<int>nums(int count){
- for (var i = 0; i < count; i++) yield return i;
- }
- static int[] numsarr = nums(10000000).ToArray();
- static int test1() {
- var sum = 0;
- foreach (var x in nums(10000000)) sum += x;
- return sum;
- }
- static int test2() {
- var sum = 0;
- for (int i = 0; i < numsarr.Length; i++) sum += numsarr[i];
- return sum;
- }
- static void Main(string[] args) {
- Console.WriteLine(test1());
- Console.WriteLine(test2());
- GC.Collect(2, GCCollectionMode.Forced);
- GC.Collect(2, GCCollectionMode.Forced);
- var sw = System.Diagnostics.Stopwatch.StartNew();
- test1();
- Console.WriteLine(sw.Elapsed);
- GC.Collect(2, GCCollectionMode.Forced);
- GC.Collect(2, GCCollectionMode.Forced);
- sw = System.Diagnostics.Stopwatch.StartNew();
- test2();
- Console.WriteLine(sw.Elapsed);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment