简体   繁体   English

C#泛型效率,更好的方法

[英]C# Generics Efficiency, a better way to do this

Ok, lets say I have classes such as the following: 好吧,我想说我有以下课程:

public class KPIObject<T> //<--This class where T is the following classes
{
    public List<T> Data { get; set; }
    public string Caption { get; set; }
}

public class KPICycleCountAccuracyData //<--There are 20 of these with different names and values
{
    public string Facility { get; set; }
    public string CCAdjustedCases { get; set; }
    public string TotalCases { get; set; }
    public string CCAdjustedPercent { get; set; }
}

Then I have: 然后我有:

public List<ReportData>> ProcessAccountReport(GetAccountReport request)
{
    var data = new List<ReportData>();
    ProcessKPI(data, request.KPICycleCountAccuracy, "KPICycleCountAccuracy"); //<-- 20 of these
    return data;
}

Here is the ProcessKPI method: 这是ProcessKPI方法:

private static void ProcessKPI<T>(List<ReportData> data, ICollection<KPIObject<T>> items, string name)
{
    if (items == null || items.Count <= 0) return;
    foreach (var item in items)
    {
        if (item.Data == null || item.Data.Count <= 0) continue;
        var temp = new List<object>();
        temp.AddRange((IEnumerable<object>)item.Data);
        data.Add(new ReportData { Data = temp, Name = name, Title = item.Caption });
    }
}

All of this works and compiles correctly, I am just wondering if this is the most efficient way of doing this. 所有这些都正常工作和编译,我只是想知道这是否是最有效的方法。

Thanks. 谢谢。

EDIT 编辑

I changed process KPI to this: 我将流程KPI更改为:

private static void ProcessKPI<T>(ICollection<ReportData> data, ICollection<KPIObject<T>> items, string name)
        {
            if (items == null || items.Count <= 0) return;
            foreach (var item in items.Where(item => item.Data != null && item.Data.Count > 0))
            {
                data.Add(new ReportData { Data = (IEnumerable<object>)item.Data, Name = name, Title = item.Caption });
            }
        }

Couple of comments 几点评论

  • There is no need to make data a ref parameter in ProcessKPI . 无需在ProcessKPI data作为ref参数。 A ref parameter is only meaningful for a class type in C# if you actually assign to it. ref参数仅对C#中的class类型有意义,如果您实际分配给它。 Here you're just modifying the object so ref doesn't by you anything except awkward call syntax 在这里你只是修改对象,所以除了笨拙的调用语法之外, ref不会出现任何问题
  • Even though Count is signed it won't ever return a negative value. 即使Count已签名,它也不会返回负值。
  • I would prefer (IEnumerable<object>)item.Data over the as IEnumerable<object> version. 我更喜欢(IEnumerable<object>)item.Data而不是as IEnumerable<object>版本。 If the latter fails it will result in an ArgumentNullException when really it's a casting issue. 如果后者失败,那么当它真的是一个转换问题时会导致ArgumentNullException

Speed Assuming you are talking about computational efficiency (ie speed), there are two operations that you might be able to improve: 速度假设您在谈论计算效率(即速度),您可以通过两种操作来改进:

First, you create a copy of the item.Data in the temp variable. 首先,在temp变量中创建item.Data的副本。 When you know that the resulting ReportData will never be modified, you may use the item.Data directly, forgoing the expensive copy operation. 当您知道永远不会修改生成的ReportData时,您可以直接使用item.Data,从而放弃昂贵的复制操作。

data.Add(new ReportData { Data = (IEnumerable<object>)item.Data, Name = name, Title = item.Caption });

Second, converting to IEnumerable<object> will probably cause unnecessary boxing/unboxing at a later point. 其次,转换为IEnumerable<object>可能会在以后导致不必要的装箱/拆箱。 See if it makes sense for your application to add a generic type parameter to ReportData , so you may instantiate it as new ReportData<KPIObject>() . 查看您的应用程序是否有必要向ReportData添加泛型类型参数,因此您可以将其实例化为new ReportData<KPIObject>() That way the compiler may do a better job of optimizing the code. 这样编译器可以更好地优化代码。

Memory By implementing your solution using continuations you may be able to process one ReportData element at a time instead of all at once, thereby reducing the memory footprint. 内存通过使用continuation实现您的解决方案,您可以一次处理一个ReportData元素,而不是一次处理所有元素,从而减少内存占用。 Have a look at the yield statement to see how to impelement such an approach. 看一下yield语句,看看如何阻止这种方法。

Other For futher code quality improvements, JaredPar's answer offers some exellent advice. 其他为了进一步提高代码质量,JaredPar的答案提供了一些优秀的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM