【问题标题】:Increase performance or Code tuning提高性能或代码调优
【发布时间】:2018-08-20 12:08:26
【问题描述】:
public static List<vwStudentExtendedData> GetStudentExtendedData(List<int> studentIds, DateTime? queryDate, int? sectionId)
{
    var summaries = new List<vwStudentExtendedData>();
    var studentsIDsForSproc = new List<int>();
    foreach (var studentId in studentIds)
    {
        var cachedCopy = CacheHelper.GetFromCache<vwStudentExtendedData>(GetStudentSummaryCacheKey(studentId, queryDate, sectionId));
        if (cachedCopy == null)
        {
            studentsIDsForSproc.Add(studentId);
        }
        else
        {
            summaries.Add(cachedCopy);
        }
    }
}

我想修改这段代码,因为我遇到了性能问题,所以我想调整代码

【问题讨论】:

  • 改写你的问题(更好地描述你所追求的)并在代码审查中提问可能是有意义的。
  • performance问题的定义是什么?你的基准是什么?有多种因素会影响性能。比如studentIds集合中的项数,CacheHelper.GetFromCache方法的代码,你使用的是什么类型的缓存。
  • 通过哪些指标?内存使用情况? CPU时间?挂钟时间?我们是否允许在对您不重要的指标上降低性能?您是否分析您的代码以确认这实际上是热点,因此更改它会明显影响性能?

标签: c# .net linq


【解决方案1】:

根据情况(应用分析或声明您的目标以让我们知道操作是否受 IO/CPU 限制)您可以使用 LINQ 或 PLINQ 或生产者-消费者模式。

对于 IO-bound 操作,LINQ 应该执行得更快。对于 CPU 限制,我更喜欢使用 Producer-Consumer 模式。

【讨论】:

    猜你喜欢
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多