首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >system.runtime.caching的性能

system.runtime.caching的性能
EN

Stack Overflow用户
提问于 2010-07-01 11:37:33
回答 1查看 22.2K关注 0票数 28

我比较了system.runtime.caching在.NET 4.0和Enterprise缓存块中的性能,令我感到惊讶的是,它在从缓存项中获取大型数据集合时表现得非常糟糕。

企业库在0,15 in中获取100个对象,在0,25 in中获取10000个对象。这对于进程内高速缓存来说是很自然的,因为实际上不需要复制任何数据(只有引用)。

.NET 4.0缓存在大约25 in内获取100个对象,在大约1500 in中获取10000个对象!与此相比,这是非常缓慢的,这让我怀疑缓存是在进程之外完成的。

我是否遗漏了一些配置选项,例如启用进程内缓存,还是企业库缓存块真的快得多?

更新

这是我的基准:

首先,我将数据从数据库加载到缓存(与基准测试分开)。

我在get方法周围使用一个计时器来测量时间,以毫秒为单位:

EnterpriseLibrary缓存

代码语言:javascript
复制
Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager _cache;

public void InitCache(){
    _cache = CacheFactory.GetCacheManager("myCacheName");
}

public void Benchmark(){
    HighPerformanceTimer timer = new HighPerformanceTimer();
    timer.Start();
    myObject o = (myObject)_cache.GetData(myCacheKey);
    timer.Stop();
    Response.Write(timer.GetAsStringInMilliseconds());
}

.NET 4.0缓存

代码语言:javascript
复制
    System.Runtime.Caching.MemoryCache _cache;

    public void InitCache(){
        _cache = new MemoryCache("myCacheName");
    }

    public void Benchmark(){
        HighPerformanceTimer timer = new HighPerformanceTimer();
        timer.Start();
        myObject o = (myObject)_cache.Get(myCacheKey);
        timer.Stop();
        Response.Write(timer.GetAsStringInMilliseconds());
    }

基准测试被执行1000次,以计算获取对象的平均时间,以确保测试的可靠性。定时器是我使用的自定义计时器,任何计算毫秒的计时器都可以。

有趣的是,"myObject“有许多引用。如果涉及到任何序列化,我会理解为什么这个对象的性能不同(比如在分布式缓存中),但这两个缓存都是进程内缓存,理论上应该工作,根本不存在重大差异。

EN

回答 1

Stack Overflow用户

发布于 2012-07-13 08:29:14

我的猜测是,缓存内容或策略的细节并不相同。如果没有看到设置或插入,很难确切说明是如何实现的。

无论如何,这两个库都有不同的性能特点,哪一个更好,显然取决于情况。

也许我的测试(下面的代码)太简单了,不可能有代表性,但是在我的机器上运行它,MemoryCache大约快10倍。

代码语言:javascript
复制
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>
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3157340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档