Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. foreach(object o in new int[] { 1,2,3 })
  2. ....
  3.  
  4. foreach(Person p in new object[] { ... })
  5. ....
  6.  
  7. namespace Iterating_types
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Text;
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. Thread.CurrentThread.Priority = ThreadPriority.Highest;
  19. Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
  20.  
  21. Stopwatch watch = new Stopwatch();
  22. int UPPER = 1000000;
  23. int[] int_arr = Enumerable.Range(1, UPPER).ToArray();
  24. List<int> int_list = Enumerable.Range(1, UPPER).ToList();
  25.  
  26. Int32[] int32_arr = Enumerable.Range(1, UPPER).ToArray();
  27.  
  28. Int64[] int64_arr = new Int64[UPPER];
  29.  
  30. IntObject[] intobject_arr = new IntObject[UPPER];
  31. List<IntObject> intobject_list = new List<IntObject>();
  32.  
  33. string[] string_arr = new string[UPPER];
  34. List<string> string_list = new List<string>();
  35.  
  36. bool[] bool_arr = new bool[UPPER];
  37. Boolean[] boolean_arr = new Boolean[UPPER];
  38. List<bool> bool_list = new List<bool>();
  39. List<Boolean> boolean_list = new List<Boolean>();
  40. // Initializing some of the above
  41. for (int i = 0; i < UPPER; i++)
  42. {
  43. int64_arr[i] = (Int64) i;
  44. string_arr[i] = i.ToString();
  45. string_list.Add(i.ToString());
  46. intobject_arr[i] = new IntObject(i);
  47. intobject_list.Add(new IntObject(i));
  48. bool_arr[i] = (i%2 ==0);
  49. boolean_arr[i] = (i%2 ==0);
  50. bool_arr[i] = (i%2 ==0);
  51. bool_list.Add(i%2 ==0);
  52.  
  53. boolean_list.Add(i%2 == 0);
  54. }
  55.  
  56. Console.WriteLine("Iterations: {0}{1}", UPPER, Environment.NewLine);
  57. Console.WriteLine("Thread priority: {0}", Thread.CurrentThread.Priority);
  58. Console.WriteLine("Process priority: {0}", Process.GetCurrentProcess().PriorityClass);
  59.  
  60. Console.WriteLine("nrArrays:t----------------------------------------------");
  61.  
  62. bool b;
  63. b = bool_arr[1];
  64. watch.Start();
  65. for (int i = 0; i < UPPER; i++)
  66. {
  67. b = bool_arr[i];
  68. }
  69. watch.Stop();
  70. Console.WriteLine("Type: booltStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  71.  
  72. watch.Start();
  73. for (int i = 0; i < UPPER; i++)
  74. {
  75. b = boolean_arr[i];
  76. }
  77. watch.Stop();
  78. Console.WriteLine("Type: BooleantStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  79.  
  80. int temp_int;
  81. temp_int = int_arr[1];
  82. watch.Start();
  83. for (int i = 0; i < UPPER; i++)
  84. {
  85. temp_int = int_arr[i];
  86. }
  87. watch.Stop();
  88. Console.WriteLine("Type: InttStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  89.  
  90. Int32 temp_int32 ;
  91. temp_int32 = int32_arr[1];
  92. watch.Reset();
  93. watch.Start();
  94. for (int i = 0; i < UPPER; i++)
  95. {
  96. temp_int32 = int32_arr[i];
  97. }
  98. watch.Stop();
  99. Console.WriteLine("Type: Int32tStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  100.  
  101. Int64 temp_int64 ;
  102. temp_int64 = int64_arr[1];
  103. watch.Reset();
  104. watch.Start();
  105. for (int i = 0; i < UPPER; i++)
  106. {
  107. temp_int64 = int64_arr[i];
  108. }
  109. watch.Stop();
  110. Console.WriteLine("Type: Int64tStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  111.  
  112. string s ;
  113. s = string_arr[1];
  114. watch.Reset();
  115. watch.Start();
  116. for (int i = 0; i < UPPER; i++)
  117. {
  118. s = string_arr[i];
  119. }
  120. watch.Stop();
  121. Console.WriteLine("Type: stringtStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  122.  
  123. temp_int = intobject_arr[1].IntValue;
  124. watch.Reset();
  125. watch.Start();
  126. for (int i = 0; i < UPPER; i++)
  127. {
  128. temp_int = intobject_arr[i].IntValue;
  129. }
  130. watch.Stop();
  131. Console.WriteLine("Type: IntObjecttStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  132.  
  133. Console.WriteLine("nrLists:t----------------------------------------------");
  134.  
  135. watch.Reset();
  136. watch.Start();
  137. foreach (var val in bool_list)
  138. {
  139. b = val;
  140. }
  141. watch.Stop();
  142. Console.WriteLine("Type: booltStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  143.  
  144. watch.Reset();
  145. watch.Start();
  146. foreach (var val in boolean_list)
  147. {
  148. b = val;
  149. }
  150. watch.Stop();
  151. Console.WriteLine("Type: BooleantStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  152.  
  153. temp_int = int_list.First();
  154. watch.Reset();
  155. watch.Start();
  156. foreach (var val in int_list)
  157. {
  158. temp_int = val;
  159. }
  160. watch.Stop();
  161. Console.WriteLine("Type: InttStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  162.  
  163. s = string_list.First();
  164. watch.Reset();
  165. watch.Start();
  166. foreach (var val in string_list)
  167. {
  168. s = val;
  169. }
  170. watch.Stop();
  171. Console.WriteLine("Type: stringtStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  172.  
  173. temp_int = intobject_list.First().IntValue;
  174. watch.Reset();
  175. watch.Start();
  176. foreach (var val in intobject_list)
  177. {
  178. temp_int = val.IntValue;
  179. }
  180. watch.Stop();
  181. Console.WriteLine("Type: IntObjecttStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
  182.  
  183. Console.WriteLine();
  184. Console.WriteLine("Hit any key to exit.");
  185. Console.ReadKey();
  186.  
  187.  
  188. }
  189. }
  190.  
  191. class IntObject
  192. {
  193. public int IntValue { get; set; }
  194.  
  195. public IntObject ()
  196. {
  197. IntValue = 0;
  198. }
  199.  
  200. public IntObject(int i)
  201. {
  202. IntValue = i;
  203. }
  204. }
  205. }
  206.  
  207. List
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement