Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach(object o in new int[] { 1,2,3 })
- ....
- foreach(Person p in new object[] { ... })
- ....
- namespace Iterating_types
- {
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- class Program
- {
- static void Main(string[] args)
- {
- Thread.CurrentThread.Priority = ThreadPriority.Highest;
- Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
- Stopwatch watch = new Stopwatch();
- int UPPER = 1000000;
- int[] int_arr = Enumerable.Range(1, UPPER).ToArray();
- List<int> int_list = Enumerable.Range(1, UPPER).ToList();
- Int32[] int32_arr = Enumerable.Range(1, UPPER).ToArray();
- Int64[] int64_arr = new Int64[UPPER];
- IntObject[] intobject_arr = new IntObject[UPPER];
- List<IntObject> intobject_list = new List<IntObject>();
- string[] string_arr = new string[UPPER];
- List<string> string_list = new List<string>();
- bool[] bool_arr = new bool[UPPER];
- Boolean[] boolean_arr = new Boolean[UPPER];
- List<bool> bool_list = new List<bool>();
- List<Boolean> boolean_list = new List<Boolean>();
- // Initializing some of the above
- for (int i = 0; i < UPPER; i++)
- {
- int64_arr[i] = (Int64) i;
- string_arr[i] = i.ToString();
- string_list.Add(i.ToString());
- intobject_arr[i] = new IntObject(i);
- intobject_list.Add(new IntObject(i));
- bool_arr[i] = (i%2 ==0);
- boolean_arr[i] = (i%2 ==0);
- bool_arr[i] = (i%2 ==0);
- bool_list.Add(i%2 ==0);
- boolean_list.Add(i%2 == 0);
- }
- Console.WriteLine("Iterations: {0}{1}", UPPER, Environment.NewLine);
- Console.WriteLine("Thread priority: {0}", Thread.CurrentThread.Priority);
- Console.WriteLine("Process priority: {0}", Process.GetCurrentProcess().PriorityClass);
- Console.WriteLine("nrArrays:t----------------------------------------------");
- bool b;
- b = bool_arr[1];
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- b = bool_arr[i];
- }
- watch.Stop();
- Console.WriteLine("Type: booltStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- b = boolean_arr[i];
- }
- watch.Stop();
- Console.WriteLine("Type: BooleantStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- int temp_int;
- temp_int = int_arr[1];
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- temp_int = int_arr[i];
- }
- watch.Stop();
- Console.WriteLine("Type: InttStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- Int32 temp_int32 ;
- temp_int32 = int32_arr[1];
- watch.Reset();
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- temp_int32 = int32_arr[i];
- }
- watch.Stop();
- Console.WriteLine("Type: Int32tStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- Int64 temp_int64 ;
- temp_int64 = int64_arr[1];
- watch.Reset();
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- temp_int64 = int64_arr[i];
- }
- watch.Stop();
- Console.WriteLine("Type: Int64tStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- string s ;
- s = string_arr[1];
- watch.Reset();
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- s = string_arr[i];
- }
- watch.Stop();
- Console.WriteLine("Type: stringtStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- temp_int = intobject_arr[1].IntValue;
- watch.Reset();
- watch.Start();
- for (int i = 0; i < UPPER; i++)
- {
- temp_int = intobject_arr[i].IntValue;
- }
- watch.Stop();
- Console.WriteLine("Type: IntObjecttStructure: Arraytticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- Console.WriteLine("nrLists:t----------------------------------------------");
- watch.Reset();
- watch.Start();
- foreach (var val in bool_list)
- {
- b = val;
- }
- watch.Stop();
- Console.WriteLine("Type: booltStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- watch.Reset();
- watch.Start();
- foreach (var val in boolean_list)
- {
- b = val;
- }
- watch.Stop();
- Console.WriteLine("Type: BooleantStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- temp_int = int_list.First();
- watch.Reset();
- watch.Start();
- foreach (var val in int_list)
- {
- temp_int = val;
- }
- watch.Stop();
- Console.WriteLine("Type: InttStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- s = string_list.First();
- watch.Reset();
- watch.Start();
- foreach (var val in string_list)
- {
- s = val;
- }
- watch.Stop();
- Console.WriteLine("Type: stringtStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- temp_int = intobject_list.First().IntValue;
- watch.Reset();
- watch.Start();
- foreach (var val in intobject_list)
- {
- temp_int = val.IntValue;
- }
- watch.Stop();
- Console.WriteLine("Type: IntObjecttStructure: Listttticks: {0}tMiliSeconds:{1}", watch.ElapsedTicks, watch.ElapsedMilliseconds);
- Console.WriteLine();
- Console.WriteLine("Hit any key to exit.");
- Console.ReadKey();
- }
- }
- class IntObject
- {
- public int IntValue { get; set; }
- public IntObject ()
- {
- IntValue = 0;
- }
- public IntObject(int i)
- {
- IntValue = i;
- }
- }
- }
- List
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement