我已經比較了.NET 4.0中的system.runtime.caching和企業庫緩存塊的性能,我驚訝地發現,從緩存項中獲取大數據集合時,它的性能會非常差。system.runtime.caching的性能
企業庫在大約0,15ms內獲取100個對象,在大約0,25ms內獲取10000個對象。這對於進程內高速緩存來說是快速和自然的,因爲實際上不需要複製任何數據(只有引用)。
.NET 4.0緩存在大約25ms內獲取100個對象,在大約1500ms內獲取10000個對象!相比之下,這非常緩慢,這讓我懷疑緩存是在流程外完成的。
我是否缺少一些配置選項,例如啓用進程內緩存,還是企業庫緩存塊真的快得多?
更新
這裏是我的標杆:
首先,我從數據庫到緩存(從基準分開)加載數據。
我周圍使用get方法計時器來衡量毫秒時間:
EnterpriseLibrary緩存
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緩存
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」有大量的參考文獻。如果涉及到任何序列化,我會理解爲什麼此對象的性能不同(如分佈式緩存),但這些都是進程內緩存,理論上應該沒有太多重大差異。
您使用MemoryCache還是基準測試自己的實現?根據msdn MemoryCache isprocess。 – ata 2010-07-01 12:06:47
我正在使用MemoryCache,沒有任何特定的配置。 – Herber 2010-07-01 12:22:31
你能告訴我們你的基準嗎? – 2010-07-01 12:33:39