Skip to content

Instantly share code, notes, and snippets.

@Thealexbarney
Created January 25, 2017 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Thealexbarney/ecd8f08aed994cd8c9076475b06cab07 to your computer and use it in GitHub Desktop.
Save Thealexbarney/ecd8f08aed994cd8c9076475b06cab07 to your computer and use it in GitHub Desktop.
Simple IList performance benchmark
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
namespace IListPerformance
{
public class Program
{
public static void Main(string[] args)
{
int iterations = 50000000;
int[] array = new int[iterations];
List<int> list = new List<int>(array);
ArrayWrapper<int> wrapper = new ArrayWrapper<int>(array);
Console.WriteLine("Warming up...");
for (int i = 0; i < 4; i++)
{
SumArrayWrapper(wrapper);
SumIList(wrapper);
SumList(list);
SumArray(array);
}
Console.WriteLine("Times are in milliseconds\n");
RunTest(x => SumArray(x), array, "Array");
RunTest(x => SumIList(x), array, "Array as IList");
RunTest(x => SumList(x), list, "List");
RunTest(x => SumIList(x), list, "List as IList");
RunTest(x => SumArrayWrapper(x), wrapper, "ArrayWrapper");
RunTest(x => SumIList(x), wrapper, "ArrayWrapper as IList");
}
private static void RunTest<T>(Action<T> test, T array, string testName)
{
Stopwatch watch = Stopwatch.StartNew();
test(array);
watch.Stop();
Console.WriteLine($"{watch.ElapsedMilliseconds} \t {testName}");
}
private static int SumArray(int[] array)
{
int result = 0;
for (int i = 0; i < array.Length; i++)
result += array[i];
return result;
}
private static int SumIList(IList<int> ilist)
{
int result = 0;
for (int i = 0; i < ilist.Count; i++)
result += ilist[i];
return result;
}
private static int SumList(List<int> list)
{
int result = 0;
for (int i = 0; i < list.Count; i++)
result += list[i];
return result;
}
private static int SumArrayWrapper(ArrayWrapper<int> wrapper)
{
int result = 0;
for (int i = 0; i < wrapper.Count; i++)
result += wrapper[i];
return result;
}
private class ArrayWrapper<T> : IList<T>
{
private readonly T[] _array;
public int Count => _array.Length;
public ArrayWrapper(T[] array)
{
_array = array;
}
public T this[int index]
{
get { return _array[index]; }
set { _array[index] = value; }
}
public IEnumerator<T> GetEnumerator() { throw new NotImplementedException(); }
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
public void Add(T item) { throw new NotImplementedException(); }
public void Clear() { throw new NotImplementedException(); }
public bool Contains(T item) { throw new NotImplementedException(); }
public void CopyTo(T[] array, int arrayIndex) { throw new NotImplementedException(); }
public bool Remove(T item) { throw new NotImplementedException(); }
public bool IsReadOnly => true;
public int IndexOf(T item) { throw new NotImplementedException(); }
public void Insert(int index, T item) { throw new NotImplementedException(); }
public void RemoveAt(int index) { throw new NotImplementedException(); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment