Guest User

Untitled

a guest
Mar 8th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace test {
  6.  
  7.     class Program {
  8.  
  9.         static IEnumerable<int>nums(int count){
  10.             for (var i = 0; i < count; i++) yield return i;
  11.         }
  12.         static int[] numsarr = nums(10000000).ToArray();
  13.  
  14.         static int test1() {
  15.             var sum = 0;
  16.             foreach (var x in nums(10000000)) sum += x;
  17.             return sum;
  18.         }
  19.         static int test2() {
  20.             var sum = 0;
  21.             for (int i = 0; i < numsarr.Length; i++) sum += numsarr[i];
  22.             return sum;
  23.         }
  24.  
  25.         static void Main(string[] args) {
  26.             Console.WriteLine(test1());
  27.             Console.WriteLine(test2());
  28.             GC.Collect(2, GCCollectionMode.Forced);
  29.             GC.Collect(2, GCCollectionMode.Forced);
  30.             var sw = System.Diagnostics.Stopwatch.StartNew();
  31.             test1();
  32.             Console.WriteLine(sw.Elapsed);
  33.             GC.Collect(2, GCCollectionMode.Forced);
  34.             GC.Collect(2, GCCollectionMode.Forced);
  35.             sw = System.Diagnostics.Stopwatch.StartNew();
  36.             test2();
  37.             Console.WriteLine(sw.Elapsed);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment