Created
December 12, 2014 15:29
-
-
Save DanielRose/d79f0da2ef61591176ce to your computer and use it in GitHub Desktop.
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
namespace MEFCachingTest | |
{ | |
using System; | |
using System.ComponentModel.Composition; | |
using System.ComponentModel.Composition.Hosting; | |
using System.Composition; | |
using System.Composition.Hosting; | |
using System.Diagnostics; | |
using System.Reflection; | |
public static class Program | |
{ | |
public static CompositionHost MEF2Container { get; set; } | |
public static ExportedClass MEF2NonCachedClass | |
{ | |
get | |
{ | |
return MEF2Container.GetExport<ExportedClass>(); | |
} | |
} | |
private static ExportedClass mef2CachedClass; | |
public static ExportedClass MEF2CachedClass | |
{ | |
get | |
{ | |
return mef2CachedClass ?? (mef2CachedClass = MEF2Container.GetExport<ExportedClass>()); | |
} | |
} | |
public static CompositionContainer SCContainer { get; set; } | |
public static ExportedClass SCNonCachedClass | |
{ | |
get | |
{ | |
return SCContainer.GetExportedValue<ExportedClass>(); | |
} | |
} | |
private static ExportedClass scCachedClass; | |
public static ExportedClass SCCachedClass | |
{ | |
get | |
{ | |
return scCachedClass ?? (scCachedClass = SCContainer.GetExportedValue<ExportedClass>()); | |
} | |
} | |
private static Lazy<ExportedClass> scLazyCachedClass; | |
public static ExportedClass SCLazyCachedClass | |
{ | |
get | |
{ | |
return scLazyCachedClass.Value; | |
} | |
} | |
public static void Main() | |
{ | |
var configuration = new ContainerConfiguration().WithAssembly(Assembly.GetExecutingAssembly()); | |
MEF2Container = configuration.CreateContainer(); | |
Console.WriteLine("MEF 2"); | |
const int Runs = 1000000; | |
var stopwatch = new Stopwatch(); | |
// Non-Cached. | |
stopwatch.Start(); | |
for (int i = 0; i < Runs; i++) | |
{ | |
var ncc = MEF2NonCachedClass; | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("Non-Cached: Time: {0}", stopwatch.Elapsed); | |
// Cached. | |
stopwatch.Restart(); | |
for (int i = 0; i < Runs; i++) | |
{ | |
var cc = MEF2CachedClass; | |
} | |
stopwatch.Stop(); | |
Console.WriteLine(" Cached: Time: {0}", stopwatch.Elapsed); | |
Console.WriteLine("System.ComponentModel.Composition"); | |
var scCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); | |
SCContainer = new CompositionContainer(scCatalog); | |
// Non-Cached. | |
stopwatch.Restart(); | |
for (int i = 0; i < Runs; i++) | |
{ | |
var ncc = SCNonCachedClass; | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("Non-Cached: Time: {0}", stopwatch.Elapsed); | |
// Cached. | |
stopwatch.Restart(); | |
for (int i = 0; i < Runs; i++) | |
{ | |
var cc = SCCachedClass; | |
} | |
stopwatch.Stop(); | |
Console.WriteLine(" Cached: Time: {0}", stopwatch.Elapsed); | |
// Lazy-Cached. | |
scLazyCachedClass = SCContainer.GetExport<ExportedClass>(); | |
stopwatch.Restart(); | |
for (int i = 0; i < Runs; i++) | |
{ | |
var cc = SCLazyCachedClass; | |
} | |
stopwatch.Stop(); | |
Console.WriteLine("LazyCached: Time: {0}", stopwatch.Elapsed); | |
} | |
} | |
[System.Composition.Export] | |
[Shared] | |
[System.ComponentModel.Composition.Export] | |
[PartCreationPolicy(CreationPolicy.Shared)] | |
public class ExportedClass | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment