Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. namespace GenericsSpeedTest
  6. {
  7. static class Program
  8. {
  9. static int BiggerInt(int i, int j)
  10. {
  11. return (i.CompareTo(j) > 0) ? i : j;
  12. }
  13.  
  14. static T Bigger<T>(T i, T j) where T : IComparable<T>
  15. {
  16. return (i.CompareTo(j) > 0) ? i : j;
  17. }
  18.  
  19. static int _count = 10000;
  20.  
  21. static void NonJeneric()
  22. {
  23. for (int j = 0; j < _count; ++j)
  24. {
  25. for (int i = 0; i < _count; ++i)
  26. {
  27. var tmp = BiggerInt(i, j);
  28. }
  29. }
  30. }
  31.  
  32. static void GenericsInt()
  33. {
  34.  
  35. for (int j = 0; j < _count; ++j)
  36. {
  37. for (int i = 0; i < _count; ++i)
  38. {
  39. var tmp = Bigger(i, j);
  40. }
  41. }
  42. }
  43.  
  44. static void Main(string[] args)
  45. {
  46. var sw = new Stopwatch();
  47.  
  48. sw.Restart();
  49. GenericsInt();
  50. sw.Stop();
  51. Console.WriteLine(sw.ElapsedMilliseconds);
  52.  
  53. sw.Restart();
  54. GenericsInt();
  55. sw.Stop();
  56. Console.WriteLine(sw.ElapsedMilliseconds);
  57.  
  58. sw.Restart();
  59. NonJeneric();
  60. sw.Stop();
  61. Console.WriteLine(sw.ElapsedMilliseconds);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement