Advertisement
Guest User

methods#4

a guest
Jun 27th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace testspeed {
  9. class Program {
  10. static int mAxInt = int.MaxValue / 1000;
  11.  
  12. static void Main(string[] args) {
  13. mAxInt = int.MaxValue / 1000;
  14.  
  15. int high_iteration = 1000;
  16. int low_iteration = 10;
  17. int high_index = 2000000;
  18. int low_index = 100;
  19. int high_arraysize = int.MaxValue / 1000;
  20. int low_arraysize = 100;
  21.  
  22. Console.WriteLine("iterations, index, arraysize");
  23. Console.WriteLine("HIGH, HIGH, HIGH");
  24. test(high_iteration, high_index, high_arraysize);
  25. Console.WriteLine("LOW, LOW, HIGH");
  26. test(low_iteration, low_index, high_arraysize);
  27. Console.WriteLine("HIGH, LOW, HIGH");
  28. test(high_iteration, low_index, high_arraysize);
  29. Console.WriteLine("LOW, HIGH, HIGH");
  30. test(low_iteration, high_index, high_arraysize);
  31.  
  32. high_index = 99;
  33. low_index = 5;
  34. Console.WriteLine("HIGH, HIGH, LOW");
  35. test(high_iteration, high_index, low_arraysize);
  36. Console.WriteLine("LOW, LOW, LOW");
  37. test(low_iteration, low_index, low_arraysize);
  38. Console.WriteLine("HIGH, LOW, LOW");
  39. test(high_iteration, low_index, low_arraysize);
  40. Console.WriteLine("LOW, HIGH, LOW");
  41. test(low_iteration, high_index, low_arraysize);
  42.  
  43. Console.ReadLine();
  44. }
  45.  
  46. public static void test(int iterations, int index, int arraySize) {
  47. int[] speedArray = new int[arraySize];
  48.  
  49. for (int i = 0; i < arraySize; i++)
  50. speedArray[i] = i;
  51.  
  52. int[] arrSpeed2 = new int[arraySize];
  53. Array.Copy(speedArray, arrSpeed2, arraySize);
  54. int[] arrSpeed3 = new int[arraySize];
  55. Array.Copy(speedArray, arrSpeed3, arraySize);
  56. int[] arrSpeed4 = new int[arraySize];
  57. Array.Copy(speedArray, arrSpeed4, arraySize);
  58.  
  59. List<int> speedList = speedArray.ToList();
  60.  
  61. Stopwatch s = new Stopwatch();
  62.  
  63. s.Reset();
  64. s.Start();
  65. for (int i = 0; i < iterations; i++) {
  66. ShiftRightAt<int>(arrSpeed2, index);
  67. }
  68. s.Stop();
  69. Console.WriteLine("Speed for ShiftRightAt: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
  70.  
  71. s.Reset();
  72. s.Start();
  73. for (int i = 0; i < iterations; i++) {
  74. shiftthing(ref speedList, index);
  75. }
  76. s.Stop();
  77. Console.WriteLine("Speed for shiftlist: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
  78.  
  79. s.Reset();
  80. s.Start();
  81. for (int i = 0; i < iterations; i++) {
  82. arrayCopy(index, ref arrSpeed3);
  83. }
  84. s.Stop();
  85. Console.WriteLine("Speed for arrayCopy: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
  86.  
  87. s.Reset();
  88. s.Start();
  89. for (int i = 0; i < iterations; i++) {
  90. MoveValueToFront(ref arrSpeed4, index);
  91. }
  92. s.Stop();
  93. Console.WriteLine("Speed for MoveValueToFront: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
  94. }
  95.  
  96. public static void moveElement(ref int[] array, int index) {
  97. int[] save = new int[] { array[index] };
  98. var rest = array.Take(index).Concat(array.Skip(index + 1));
  99. array = save.Concat(rest).ToArray();
  100. }
  101.  
  102. public static void ShiftRightAt<T>(T[] array, int index) {
  103. var element = array[index]; // take out the element
  104.  
  105. for (int i = index; i > 0; i--) {
  106. array[i] = array[i - 1];
  107. }
  108.  
  109. array[0] = element;
  110. }
  111.  
  112. public static void shiftthing(ref List<int> list, int index) {
  113. var mem = list[index];
  114. list.Remove(index);
  115. list.Insert(0, mem);
  116. }
  117.  
  118. public static void arrayCopy(int elementAt, ref int[] array) {
  119. int saved = array[elementAt];
  120. Array.Copy(array, elementAt + 1, array, elementAt, array.Length - elementAt - 1);
  121. Array.Copy(array, 0, array, 1, array.Length - 1);
  122. array[0] = saved;
  123. }
  124.  
  125. public static void MoveValueToFront(ref int[] values, int searchValue) {
  126. var rest = values.TakeWhile(v => v != searchValue).ToArray();
  127.  
  128. if (rest.Length == values.Length) {
  129. return;
  130. }
  131.  
  132. values[0] = searchValue;
  133. rest.CopyTo(values, 1);
  134.  
  135. return;
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement