【发布时间】:2011-07-03 01:50:28
【问题描述】:
我有以下代码
private void LoadIntoMemory()
{
//Init large HashSet
HashSet<document> hsAllDocuments = new HashSet<document>();
//Get first rows from database
List<document> docsList = document.GetAllAboveDocID(0, 500000);
//Load objects into dictionary
foreach (document d in docsList)
{
hsAllDocuments.Add(d);
}
Application["dicAllDocuments"] = hsAllDocuments;
}
private HashSet<document> documentHits(HashSet<document> hsRawHit, HashSet<document> hsAllDocuments, string query, string[] queryArray)
{
int counter = 0;
const int maxCount = 1000;
foreach (document d in hsAllDocuments)
{
//Headline
if (d.Headline.Contains(query))
{
if (counter >= maxCount)
break;
hsRawHit.Add(d);
counter++;
}
//Description
if (d.Description.Contains(query))
{
if (counter >= maxCount)
break;
hsRawHit.Add(d);
counter++;
}
//splitted query word by word
//string[] queryArray = query.Split(' ');
if (queryArray.Count() > 1)
{
foreach (string q in queryArray)
{
if (d.Headline.Contains(q))
{
if (counter >= maxCount)
break;
hsRawHit.Add(d);
counter++;
}
//Description
if (d.Description.Contains(q))
{
if (counter >= maxCount)
break;
hsRawHit.Add(d);
counter++;
}
}
}
}
return hsRawHit;
}
首先我将所有数据加载到一个哈希集中(通过应用程序供以后使用) - 运行良好 - 完全可以对我正在做的事情放慢速度。
将在 C# 中运行 4.0 框架(无法使用异步内容更新到 4.0 的新升级)。
documentHits 方法在我当前的设置中运行相当慢 - 考虑到它都在内存中。我可以做些什么来加快这个方法?
例子会很棒 - 谢谢。
【问题讨论】:
-
您的分析器运行说什么是最慢的?从那开始。它有多慢?您对“足够快”的预算是多少?
-
文档数量可能不是线性的。
-
既然你只是遍历内容,为什么还要费心使用 HashSet。
-
他使用HashSet来防止重复,这是错误的方式。
标签: c# performance hashtable contains hashset