fork(2) download
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. public class Program {
  9. static int[] array = new int[] { 1, 3 };
  10.  
  11. static long benchMathMax(int time) {
  12. var stopWatch = new Stopwatch();
  13. stopWatch.Start();
  14. for (int i = 0; i < time; i++) {
  15. var r = Math.Max(array[0], array[1]);
  16. }
  17. return stopWatch.ElapsedMilliseconds;
  18. }
  19.  
  20. static long benchEnumerableMax(int time) {
  21. var stopWatch = new Stopwatch();
  22. stopWatch.Start();
  23. for (int i = 0; i < time; i++) {
  24. var r = array.Max();
  25. }
  26. return stopWatch.ElapsedMilliseconds;
  27. }
  28.  
  29. static long benchArrayLength(int time) {
  30. var stopWatch = new Stopwatch();
  31. stopWatch.Start();
  32. for (int i = 0; i < time; i++) {
  33. var r = array.Length;
  34. }
  35. return stopWatch.ElapsedMilliseconds;
  36. }
  37.  
  38. static long benchEnumerableCount(int time) {
  39. var stopWatch = new Stopwatch();
  40. stopWatch.Start();
  41. for (int i = 0; i < time; i++) {
  42. var r = array.Count();
  43. }
  44. return stopWatch.ElapsedMilliseconds;
  45. }
  46.  
  47. static void Main(string[] args) {
  48. var array = new[] { 1, 3 };
  49.  
  50. int time = 10000000;
  51.  
  52. Console.WriteLine("Math.Max : {0}ms", benchMathMax(time));
  53. Console.WriteLine("Enumerable.Max : {0}ms", benchEnumerableMax(time));
  54. Console.WriteLine("Array.Length : {0}ms", benchArrayLength(time));
  55. Console.WriteLine("Enumerable.Count: {0}ms", benchEnumerableCount(time));
  56. }
  57. }
  58.  
Success #stdin #stdout 1.63s 34208KB
stdin
Standard input is empty
stdout
Math.Max        : 24ms
Enumerable.Max  : 1363ms
Array.Length    : 8ms
Enumerable.Count: 218ms