fork(1) 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. }
  18. }
  19.  
  20. static long intSum(int[] array)
  21. {
  22. long sum = 0;
  23. for (int i = 0; i < array.Length; i += 8) sum += array[i];
  24. return sum;
  25. }
  26.  
  27. static long longSum(long[] array)
  28. {
  29. long sum = 0;
  30. for (int i = 0; i < array.Length; i += 8) sum += array[i];
  31. return sum;
  32. }
  33.  
  34. static void Measure(Func<long> action, string description)
  35. {
  36. Stopwatch stopwatch = new Stopwatch();
  37. stopwatch.Start();
  38. long result = action();
  39. stopwatch.Stop();
  40. Console.WriteLine($"{description} took {stopwatch.ElapsedMilliseconds} ms ({result})");
  41. }
  42. }
  43. }
Success #stdin #stdout 3.36s 1969172KB
stdin
Standard input is empty
stdout
 int sum took 25 ms (624999950000000)
long sum took 49 ms (624999950000000)
 int sum took 23 ms (624999950000000)
long sum took 49 ms (624999950000000)
 int sum took 23 ms (624999950000000)
long sum took 48 ms (624999950000000)
 int sum took 23 ms (624999950000000)
long sum took 48 ms (624999950000000)