Last active
January 20, 2016 15:26
-
-
Save zaus/2ce7e8a4f1e72124537f to your computer and use it in GitHub Desktop.
Perf test of looping through Headers from http://stackoverflow.com/questions/6439225/how-to-loop-through-webheadercollection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var headers = new System.Net.WebHeaderCollection(); | |
headers.Add("xxx", "yyy"); | |
headers.Add("zzz", "f&&ff"); | |
headers.Add("xxx", "ttt"); | |
headers.Add("yyy", "33,3"); | |
headers.Dump("raw"); | |
headers.ToString().Dump("string'd"); | |
dumpHeaders(headers); | |
toTuple(headers).Dump("Tuples"); | |
toDict(headers).Dump("Dict"); | |
toArray(headers).Dump("Array"); | |
// dumps results and timestamps for each "test" | |
new Perf<object> { | |
{ "dict", n => (object) loop(toDict(headers)) }, | |
{ "array", n => (object) loop(toArray(headers)) }, | |
{ "enumerable", n => (object) loop(toEnumerable(headers)) }, | |
{ "tuple", n => (object) loop(toTuple(headers)) }, | |
}.Vs("Loop All"); | |
// dumps results and timestamps for each "test" | |
new Perf<object> { | |
{ "dict", n => (object) loop(toDict(headers), shortCircuit) }, | |
{ "array", n => (object) loop(toArray(headers), shortCircuit) }, | |
{ "enumerable", n => (object) loop(toEnumerable(headers), shortCircuit) }, | |
{ "tuple", n => (object) loop(toTuple(headers), shortCircuit) }, | |
}.Vs("Premature stop"); | |
} | |
// Define other methods and classes here | |
bool shortCircuit<T>(T instance) { return true; } | |
T loop<T>(IEnumerable<T> list, Func<T, bool> stop = null) { | |
T result = default(T); | |
foreach(var o in list) { | |
result = o; | |
if(null != stop && stop(result)) break; | |
} | |
return result; | |
} | |
Dictionary<string, string[]> toDict(System.Net.WebHeaderCollection headers) { | |
return Enumerable.Range(0, headers.Count).ToDictionary(i => headers.Keys[i], headers.GetValues); | |
} | |
IEnumerable<KeyValuePair<string, string[]>> toEnumerable(System.Net.WebHeaderCollection headers) { | |
foreach(var key in headers.AllKeys) { | |
yield return new KeyValuePair<string, string[]>(key, headers.GetValues(key)); | |
} | |
} | |
KeyValuePair<string, string>[] toArray(System.Net.WebHeaderCollection headers) { | |
string[] keys = headers.AllKeys; | |
var keyVals = new KeyValuePair<string, string>[keys.Length]; | |
for (int i = 0; i < keys.Length; i++) | |
keyVals[i] = new KeyValuePair<string, string>(keys[i], headers[keys[i]]); | |
return keyVals; | |
} | |
IEnumerable<Tuple<string, string>> toTuple(System.Net.WebHeaderCollection headers) { | |
return Enumerable | |
.Range(0, headers.Count) | |
.SelectMany(i => headers.GetValues(i) | |
.Select(v => Tuple.Create(headers.GetKey(i), v))); | |
} | |
void dumpHeaders(System.Net.WebHeaderCollection headers) { | |
for(int i = 0; i < headers.Count; ++i) { | |
string header = headers.GetKey(i); | |
foreach(string value in headers.GetValues(i)) { | |
Console.WriteLine("{0}: {1}", header, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment