Performance of system.runtime.caching

前端 未结 1 2011

I have compared the performance of system.runtime.caching in .NET 4.0 and the Enterprise Library Caching Block and to my surprise it performs terribly in comparison when fet

相关标签:
1条回答
  • 2020-12-08 05:40

    My guess is that the details of your cache contents or policies are not the same. Without seeing the setup, or the inserts, it's hard to say exactly how.

    Regardless, the two libraries have different performance characteristics, and which one is better clearly depends on the situation.

    Probably my test (code below) is too simple to be representative, but with it running on my machine, MemoryCache is roughly 10x faster.

    class Program
    {        
        const string myCacheKey = "foo";
        static ICacheManager _elCache;        
        static MemoryCache _rtCache;
        public static void InitCache()
        {            
            _elCache = CacheFactory.GetCacheManager();
            _elCache.Add(myCacheKey, new object());
    
            _rtCache = new MemoryCache("cache");
            _rtCache.Add(myCacheKey, new object(), new CacheItemPolicy());
        }
        public static string ElBenchmark(int n)
        {
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < n; i++)
            {
                object o = _elCache.GetData(myCacheKey);
            }
            timer.Stop();
            return timer.ElapsedTicks.ToString();
        }
        public static string RtBenchmark(int n)
        {
            Stopwatch timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < n; i++)
            {
                object o = _rtCache.Get(myCacheKey);
            }
            timer.Stop();
            return timer.ElapsedTicks.ToString();
        }
        static void Main(string[] args)
        {
            while (true)
            {
                InitCache();
                StringBuilder sb = new StringBuilder();
                System.Diagnostics.Debug.Write("EL: " + ElBenchmark(10000));
                System.Diagnostics.Debug.Write("\t");
                System.Diagnostics.Debug.Write("RT: " + RtBenchmark(10000));
                System.Diagnostics.Debug.Write("\r\n");
            }
        }
    }
    
    
    <?xml version="1.0"?>
    <configuration>
    
      <configSections>
        <section name="cachingConfiguration"
             type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
      </configSections>
      <cachingConfiguration defaultCacheManager="MyCacheManager">
        <cacheManagers>
          <add name="MyCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           expirationPollFrequencyInSeconds="60"
           maximumElementsInCacheBeforeScavenging="50000"
           numberToRemoveWhenScavenging="1000"
           backingStoreName="NullBackingStore" />
        </cacheManagers>
        <backingStores>
          <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           name="NullBackingStore" />
        </backingStores>
      </cachingConfiguration>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>  
    </configuration>
    
    0 讨论(0)
提交回复
热议问题