Skip to content

Instantly share code, notes, and snippets.

@zaus
Last active January 20, 2016 15:26
Show Gist options
  • Save zaus/2ce7e8a4f1e72124537f to your computer and use it in GitHub Desktop.
Save zaus/2ce7e8a4f1e72124537f to your computer and use it in GitHub Desktop.

Revisions

  1. zaus renamed this gist Jan 20, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. zaus created this gist Jan 13, 2016.
    75 changes: 75 additions & 0 deletions perf.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    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);
    }
    }
    }