[英]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 几点评论
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
不会出现任何问题 Count
is signed it won't ever return a negative value. Count
已签名,它也不会返回负值。 (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.