fork download
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4.  
  5. namespace MemoryPerformance
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. const int count = 100000000;
  12. int[] intArray = Enumerable.Range(0, count).ToArray();
  13. long[] longArray = intArray.Select(x => (long)x).ToArray();
  14. for (int i = 0; i < 4; i++) {
  15. Measure(() => intSum(intArray), " int sum");
  16. Measure(() => longSum(longArray), "long sum");
  17. Measure(() => noSum(longArray), " no sum");
  18. }
  19. }
  20.  
  21. static long intSum(int[] array)
  22. {
  23. long sum = 0;
  24. for (int i = 0; i < array.Length; i ++) sum += array[i];
  25. return sum;
  26. }
  27.  
  28. static long longSum(long[] array)
  29. {
  30. long sum = 0;
  31. for (int i = 0; i < array.Length; i ++) sum += array[i];
  32. return sum;
  33. }
  34.  
  35. static long noSum(long[] array)
  36. {
  37. long sum = 0;
  38. for (int i = 0; i < array.Length; i ++) sum += i;
  39. return sum;
  40. }
  41.  
  42. static void Measure(Func<long> action, string description)
  43. {
  44. Stopwatch stopwatch = new Stopwatch();
  45. stopwatch.Start();
  46. long result = action();
  47. stopwatch.Stop();
  48. Console.WriteLine($"{description} took {stopwatch.ElapsedMilliseconds} ms ({result})");
  49. }
  50. }
  51. }
Success #stdin #stdout 3.96s 1969260KB
stdin
Standard input is empty
stdout
 int sum took 84 ms (4999999950000000)
long sum took 81 ms (4999999950000000)
  no sum took 52 ms (4999999950000000)
 int sum took 84 ms (4999999950000000)
long sum took 78 ms (4999999950000000)
  no sum took 51 ms (4999999950000000)
 int sum took 84 ms (4999999950000000)
long sum took 77 ms (4999999950000000)
  no sum took 51 ms (4999999950000000)
 int sum took 83 ms (4999999950000000)
long sum took 76 ms (4999999950000000)
  no sum took 51 ms (4999999950000000)