![](/img/trans.png)
[英]How to improve performance and speed in my code (especially in double.Parse)?
[英]How to improve speed and performance on refresh and update ObserveableCollection
我正在尝试使用从SQL过程检索到的新项目更新和刷新现有的ObservableCollection(列表中大约有2.3mill个对象)。 当我获得最新类别时,我会收到一份包含约72000个项目的清单。 我创建的用于更新和刷新集合的函数确实运行良好,但是在许多更新的元素上,它变得非常缓慢并且性能很差。 我该如何以更有效和更好的方式做到这一点?
public async Task UpdateAndRefreshContractMetaDataCollection(ObservableCollection<Category> Categories)
{
Type type = typeof(Category);
var updatedCategories = await GetLatestUpdatedCategories();
if (updatedCategories .Count != 0)
{
try
{
int i = 0;
foreach (var category in updatedCategories )
{
var categoryCopy = Categories.FirstOrDefault(_ => _.ID== category.ID);
if (categoryCopy != null)
{
i+=1;
if (!compareLogic.Compare(categoryCopy , category ).AreEqual)
{
mapper.Map<Category, Category>(category , categoryCopy );
}
else
{
Categories.Add(category );
}
}
}
}
catch (Exception e) {
MessageBox.Show("Error updating categories", e.Message);
}
}
}
FirstOrDefault是瓶颈的很大一部分。 对于更新类别中的每个项目,您都要遍历主集合。 使用字典会更快。
尝试这个。
public async Task UpdateAndRefreshContractMetaDataCollection(ObservableCollection<Category> Categories)
{
Type type = typeof(Category);
var updatedCategories = await GetLatestUpdatedCategories();
var CatDict = Categories.ToDictionary(c => c.ID);
if (updatedCategories.Count != 0)
{
try
{
int i = 0;
foreach (var category in updatedCategories)
{
var categoryCopy = CatDict.ContainsKey(category.ID) ? CatDict[category.ID] : null;
if (categoryCopy != null)
{
i += 1;
if (!compareLogic.Compare(categoryCopy, category).AreEqual)
{
mapper.Map<Category, Category>(category, categoryCopy);
}
else
{
Categories.Add(category);
}
}
}
}
catch (Exception e)
{
MessageBox.Show("Error updating categories", e.Message);
}
}
}
我尝试了伪像工作解决方案。 这更是以声明性的方式使用Linq进行编程,而不是命令性的。 抱歉,我没有使用与您的代码片段相同的对象。 但是尝试用我自己的对象使它更易于理解。
class Program
{
static void Main(string[] args)
{
// Your categires been passed to the method (new categories I assume)
List<int> categories = new List<int>();
// Updated categores you retrieved from the database.
ObservableCollection<int> updatedCategories = new ObservableCollection<int>();
// Select all the none matching categires to be added in the <mapper.Map>
var notmatching = (from u in updatedCategories join n in categories on u equals n where !Comparer(u, n) select u).ToList();
// Select all the matching categires to be added in the <categories> collection
var matching = (from u in updatedCategories join n in categories on u equals n where Comparer(u, n) select u).ToList();
// Addiing notmaching categories to you map
notmatching.ForEach(i => Map(i));
// Adding matching categies to your existing Categories collection
categories.AddRange(matching);
}
// Represent 'compareLogic.Compare(categoryCopy , category ).AreEqual'
static bool Comparer(int a, int b) {
return a == b;
}
// Represent mapper.Map<Category, Category>(category , categoryCopy );
static void Map(int a) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.