Skip to content

Instantly share code, notes, and snippets.

@mhusseini
Last active May 24, 2016 03:38
Show Gist options
  • Save mhusseini/bfd816ca566ee0bb88e8 to your computer and use it in GitHub Desktop.
Save mhusseini/bfd816ca566ee0bb88e8 to your computer and use it in GitHub Desktop.
Timings test for NamedFormats: Object vs ExpandoObject
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
namespace guerillacoder
{
internal class Program
{
private static void Main(string[] args)
{
var n = 1000000;
var dtime = MeasureDictionaryTime(n);
var cdtime = MeasureConcurrentDictionaryTime(n);
var otime = TestWithObject(n) - cdtime;
var eotime = TestWithExpandoObject(n) - cdtime - dtime;
Console.WriteLine();
Console.WriteLine("Timings for object with " + n + " iterations: " + otime);
Console.WriteLine("Timings for expando object with " + n + " iterations: " + eotime);
}
public static long MeasureConcurrentDictionaryTime(int n)
{
var cacheKey = n.GetType().GetHashCode() + "{Test1}, {Test2}, {Test3}";
var precompiledExpressions = new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase);
precompiledExpressions.TryAdd(cacheKey, new object());
// If we already have a compiled expression, just execute it.
object o;
var sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
if (precompiledExpressions.TryGetValue(cacheKey, out o))
{
continue;
}
}
sw.Stop();
Console.WriteLine("Concurrent Dictionary time: " + sw.ElapsedMilliseconds);
return sw.ElapsedMilliseconds;
}
public static long MeasureDictionaryTime(int n)
{
var cacheKey = n.GetType().GetHashCode() + "{Test1}, {Test2}, {Test3}";
var precompiledExpressions = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
precompiledExpressions.Add(cacheKey, new object());
// If we already have a compiled expression, just execute it.
object o;
var sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
if (precompiledExpressions.TryGetValue(cacheKey, out o))
{
continue;
}
}
sw.Stop();
Console.WriteLine("Dictionary time: " + sw.ElapsedMilliseconds);
return sw.ElapsedMilliseconds;
}
private static long TestWithExpandoObject(int n)
{
var pattern = "{Test1}, {Test2}, {Test3}";
dynamic o = new ExpandoObject();
o.Test1 = "abc";
o.Test2 = "abc";
o.Test3 = "abc";
var s = NamedFormat.Format(pattern, o);
var sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
s = NamedFormat.Format(pattern, o);
}
sw.Stop();
Console.WriteLine("Test with expando object: " + sw.ElapsedMilliseconds);
return sw.ElapsedMilliseconds;
}
private static long TestWithObject(int n)
{
var pattern = "{Test1}, {Test2}, {Test3}";
var o = new
{
Test1 = "abc",
Test2 = "abc",
Test3 = "abc"
};
var s = NamedFormat.Format(pattern, o);
var sw = Stopwatch.StartNew();
for (var i = 0; i < n; i++)
{
s = NamedFormat.Format(pattern, o);
}
sw.Stop();
Console.WriteLine("Test with object: " + sw.ElapsedMilliseconds);
return sw.ElapsedMilliseconds;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment