Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace testspeed {
- class Program {
- static int mAxInt = int.MaxValue / 1000;
- static void Main(string[] args) {
- mAxInt = int.MaxValue / 1000;
- int high_iteration = 1000;
- int low_iteration = 10;
- int high_index = 2000000;
- int low_index = 100;
- int high_arraysize = int.MaxValue / 1000;
- int low_arraysize = 100;
- Console.WriteLine("iterations, index, arraysize");
- Console.WriteLine("HIGH, HIGH, HIGH");
- test(high_iteration, high_index, high_arraysize);
- Console.WriteLine("LOW, LOW, HIGH");
- test(low_iteration, low_index, high_arraysize);
- Console.WriteLine("HIGH, LOW, HIGH");
- test(high_iteration, low_index, high_arraysize);
- Console.WriteLine("LOW, HIGH, HIGH");
- test(low_iteration, high_index, high_arraysize);
- high_index = 99;
- low_index = 5;
- Console.WriteLine("HIGH, HIGH, LOW");
- test(high_iteration, high_index, low_arraysize);
- Console.WriteLine("LOW, LOW, LOW");
- test(low_iteration, low_index, low_arraysize);
- Console.WriteLine("HIGH, LOW, LOW");
- test(high_iteration, low_index, low_arraysize);
- Console.WriteLine("LOW, HIGH, LOW");
- test(low_iteration, high_index, low_arraysize);
- Console.ReadLine();
- }
- public static void test(int iterations, int index, int arraySize) {
- int[] speedArray = new int[arraySize];
- for (int i = 0; i < arraySize; i++)
- speedArray[i] = i;
- int[] arrSpeed2 = new int[arraySize];
- Array.Copy(speedArray, arrSpeed2, arraySize);
- int[] arrSpeed3 = new int[arraySize];
- Array.Copy(speedArray, arrSpeed3, arraySize);
- int[] arrSpeed4 = new int[arraySize];
- Array.Copy(speedArray, arrSpeed4, arraySize);
- List<int> speedList = speedArray.ToList();
- Stopwatch s = new Stopwatch();
- s.Reset();
- s.Start();
- for (int i = 0; i < iterations; i++) {
- ShiftRightAt<int>(arrSpeed2, index);
- }
- s.Stop();
- Console.WriteLine("Speed for ShiftRightAt: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
- s.Reset();
- s.Start();
- for (int i = 0; i < iterations; i++) {
- shiftthing(ref speedList, index);
- }
- s.Stop();
- Console.WriteLine("Speed for shiftlist: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
- s.Reset();
- s.Start();
- for (int i = 0; i < iterations; i++) {
- arrayCopy(index, ref arrSpeed3);
- }
- s.Stop();
- Console.WriteLine("Speed for arrayCopy: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
- s.Reset();
- s.Start();
- for (int i = 0; i < iterations; i++) {
- MoveValueToFront(ref arrSpeed4, index);
- }
- s.Stop();
- Console.WriteLine("Speed for MoveValueToFront: " + s.ElapsedMilliseconds + ", ticks: " + s.ElapsedTicks);
- }
- public static void moveElement(ref int[] array, int index) {
- int[] save = new int[] { array[index] };
- var rest = array.Take(index).Concat(array.Skip(index + 1));
- array = save.Concat(rest).ToArray();
- }
- public static void ShiftRightAt<T>(T[] array, int index) {
- var element = array[index]; // take out the element
- for (int i = index; i > 0; i--) {
- array[i] = array[i - 1];
- }
- array[0] = element;
- }
- public static void shiftthing(ref List<int> list, int index) {
- var mem = list[index];
- list.Remove(index);
- list.Insert(0, mem);
- }
- public static void arrayCopy(int elementAt, ref int[] array) {
- int saved = array[elementAt];
- Array.Copy(array, elementAt + 1, array, elementAt, array.Length - elementAt - 1);
- Array.Copy(array, 0, array, 1, array.Length - 1);
- array[0] = saved;
- }
- public static void MoveValueToFront(ref int[] values, int searchValue) {
- var rest = values.TakeWhile(v => v != searchValue).ToArray();
- if (rest.Length == values.Length) {
- return;
- }
- values[0] = searchValue;
- rest.CopyTo(values, 1);
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement