fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. class Program {
  7.  
  8. static void Main(string[] args) {
  9. var source = Enumerable.Range(0, 10000000);
  10. var sw = Stopwatch.StartNew();
  11. var r = Windowed(source, 3).Count();
  12. sw.Stop();
  13. Console.WriteLine(r);
  14. Console.WriteLine(sw.Elapsed);
  15. }
  16.  
  17. static IEnumerable<T[]> Windowed<T>(IEnumerable<T> source, int windowSize) {
  18. if (source == null)
  19. throw new ArgumentNullException("source");
  20. if (windowSize <= 0)
  21. throw new ArgumentOutOfRangeException("windowSize");
  22. var arr = new T[windowSize];
  23. int r = (windowSize - 1);
  24. int i = 0;
  25. foreach (var item in source) {
  26. arr[i] = item;
  27. i = (i + 1) % windowSize;
  28. if (r == 0) {
  29. var arrR = new T[windowSize];
  30. for (int j = 0; j < windowSize; j++)
  31. arrR[j] = arr[(i + j) % windowSize];
  32. yield return arrR;
  33. } else {
  34. r--;
  35. }
  36. }
  37. }
  38. }
  39.  
Success #stdin #stdout 3.12s 37080KB
stdin
Standard input is empty
stdout
9999998
00:00:03.0943769