Skip to content

Instantly share code, notes, and snippets.

@doubleday
Created August 19, 2015 17:42
Show Gist options
  • Save doubleday/9f4b4b3eba1a9a87ef41 to your computer and use it in GitHub Desktop.
Save doubleday/9f4b4b3eba1a9a87ef41 to your computer and use it in GitHub Desktop.
Quick and dirt c# list test
public static class Dev
{
public static void Null(object val)
{
if (val == null)
{
throw new InvalidOperationException();
}
}
}
public class Container
{
readonly List<string> _things;
public Container()
{
const int itemCount = 32;
_things = Enumerable
.Range(0, itemCount)
.Select(i => string.Format("{0}-value", i))
.ToList();
}
#region Iteration
public void IndexedInteration()
{
for (var i = 0; i < _things.Count; ++i)
{
Dev.Null(_things[i]);
}
}
public void ForEachIteration()
{
foreach (var thing in _things)
{
Dev.Null(thing);
}
}
public void ListIteration()
{
_things.ForEach(Dev.Null);
}
#endregion
#region Find
public void IndexedFind(string needle)
{
string val = null;
for (var i = 0; i < _things.Count; ++i)
{
if (_things[i] == needle)
{
val = _things[i];
break;
}
}
Dev.Null(val);
}
public void ForEachFind(string needle)
{
string val = null;
foreach (var thing in _things)
{
if (thing == needle)
{
val = thing;
break;
}
}
Dev.Null(val);
}
public void ListFind(string needle)
{
var val = _things.Find(thing => thing == needle);
Dev.Null(val);
}
public void LinqFind(string needle)
{
var val = _things.First(thing => thing == needle);
Dev.Null(val);
}
#endregion
#region Transformation
public void IndexedTransformation()
{
var otherList = new List<string>();
for (var i = 0; i < _things.Count; ++i)
{
otherList.Add(_things[i] + "_transformed");
}
Dev.Null(otherList);
}
public void ForEachTransformation()
{
var otherList = new List<string>();
foreach (var thing in _things)
{
otherList.Add(thing + "_transformed");
}
Dev.Null(otherList);
}
public void LinqTransformation()
{
var otherList = _things.Select(t => t + "_transformed").ToList();
Dev.Null(otherList);
}
#endregion
}
public class Scratch
{
const int TestIterations = 10000;
readonly Stopwatch _watch = new Stopwatch();
// Use this for initialization
public void Start()
{
var container = new Container();
Meassure("IndexedInteration", container.ForEachIteration);
Meassure("ForEachIteration", container.ForEachIteration);
Meassure("ListIteration", container.ListIteration);
Meassure("IndexedFind", () => container.IndexedFind("17-value"));
Meassure("ForEachFind", () => container.ForEachFind("17-value"));
Meassure("ListFind", () => container.ListFind("17-value"));
Meassure("LinqFind", () => container.LinqFind("17-value"));
Meassure("IndexedTransformation", container.IndexedTransformation);
Meassure("ForEachTransformation", container.ForEachTransformation);
Meassure("LinqTransformation", container.LinqTransformation);
}
void Meassure(string taskName, Action action)
{
_watch.Reset();
_watch.Start();
for (var i = 0; i < TestIterations; ++i)
{
action();
}
_watch.Stop();
Console.WriteLine(taskName + "\t" + _watch.Elapsed.TotalMilliseconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment