c# 高效的 If-Then 块
c# Efficient If-Then block
正在编写一个 C# 程序,我试图在其中确定某些内容是否具有正确的 ID (ClVal)。正在使用 Interop 从 Excel 电子表格读取数据,并将其传输到数据 table。
Excel 电子表格包含命名对象列表、对象的所有者及其所有者的 ClVal。
假设 PeoplePower, INC.(我当场编造的名称)拥有三台计算机。那三台计算机的 ClVal 应该都是 0000_Peop。我需要做的是确保如果这些计算机归 PeoplePower, INC. 所有,则它们具有正确的 ClVal。
我的局限之一是我不能简单地将 ClVal 的子字符串与所有者名称进行比较。这是因为如果我从 ClVal 中取出子字符串 "Peop" 并在所有者字段中查找它,对于名称中某处带有 "Peop" 的任何内容,它将 return 为真(存在)。我想限制任何误报。
为了增加另一层复杂性,只有大量机器的所有者才能获得自己独特的 ClVal。对于其他所有人,他们的 ClVal 为 "other"。但是,有时大公司的机器会被赋予"other" ClVal。
从整体上看,基本上有以下三种情况我们需要检查:
If the computer has the correct ClVal (e.g a PeoplePower computer has the ClVal "0000_Peop"), we should assign that cell's value to 1. This helps operators identify machines with the correct ID at a glance, and allows us to enumerate correctly identified machines.
If the computer has an incorrect ClVal (i.e. a PeoplePower computer has the ClVal "Other"), we should assign that cell's value to 0. This helps operators identify "minor errors" - needs to be fixed but it can wait.
If the computer does not have a ClVal or it has the ClVal of another owner, the cells value should be a -1. This helps operators identify "major errors" that need to be fixed immediately.
到目前为止,我想到了一种方法,但我想知道是否有任何 better/more 有效的选择。我目前有大约 3500 行信息需要排序,而且这个数字还在稳步上升,所以我需要一个能够处理更多行的解决方案。
想法:
string ClVal = Convert.ToString(((Excel.Range)excelStuff.xlWorksheet.Cells[rowIndex, 2]).Value2);
string name = Convert.ToString(((Excel.Range)excelStuff.xlWorksheet.Cells[rowIndex, 5]).Value2);
if (name.Contains("PeoplePower"))
{
string ProperClVal = "0000_Peop";
row[4] = testClVal(ClVal, ProperClVal);
}
//Repeat with else if for all of the major owners
else
{
if (ClVal == "Other")
{
row[4] = 1;
}
else
{
row[4] = -1;
}
}
//while循环外
private int testClVal(String reportedClVal, String ProperClVal)
{
if (reportedClVal == ProperClVal)
{
return 1;
}
else if (reportedClVal == "Other")
{
return 0;
}
else
{
return -1;
}
}
这是功能性的,但它是一堆 if-then 语句,如果我的 excel 电子表格由于错误数据(发生在一些电脑)。
是否有更多 efficient/better/faster 方法来完成此任务?对于 3800 行,它将数据的加载时间table 从 1.5 分钟增加到 2.5 -3 分钟。
怎么样
row[4] = ClVal == "0000_Peop" ? 1 : ClVal == "Other" ? 0 : -1
此外,我还想提一下风格方面的一些问题。
- 请使用
string
,不要到处使用 String
。这是 C# 中公认的约定。
- 您的局部变量应该是驼峰式命名并且以小写字母开头。
ProperClVal
,例如,对我来说看起来像一个 class 名字。
这是您可以用于此类任务的最快的算法类别,因为您的程序需要接触每一行。因此,您正在考虑必须优化程序的其他部分或其他模式。
使用复杂性术语 类 (https://en.wikipedia.org/wiki/Complexity_class) 这是一个 O(n) 任务,这意味着 2000 个条目所花费的时间是 1000 个条目的两倍。您想要执行的操作是检查每个值并做出不(非常)依赖于该值有多大或有多少值的决定。在伪代码中:
1) for every Row in Sheet
2) lookup a fixed number of values
3) do a fixed number of comparisons
4) assign a result
5) end loop
第 1 行将被执行 n
次。对于循环中的每次迭代,您将执行一组成本不变的操作;我们会说成本是 1
。这意味着总数 cost/complexity 将是 n
*1
=n
,这就是为什么这是 O(n)。
为了加快速度,我会考虑使用 Excel 互操作以外的东西(它比 NPOI https://npoi.codeplex.com/). Also see if you can refactor the problem: perhaps you don't really need to compute all the values at once but could "lazily-load" them https://en.wikipedia.org/wiki/Lazy_loading 慢,否则会延迟计算
一次从 excel 读取一个值真的很慢。您应该一步读取所有数据,例如:
public object[,] GetArray(int topRow, int rows, int columns)
{
Range c1 = (Range)Worksheet.Cells[topRow + 1, 1];
Range c2 = (Range)Worksheet.Cells[topRow + 1 + rows - 1, columns];
Range range = Worksheet.get_Range(c1, c2);
return range.Value;
}
正在编写一个 C# 程序,我试图在其中确定某些内容是否具有正确的 ID (ClVal)。正在使用 Interop 从 Excel 电子表格读取数据,并将其传输到数据 table。
Excel 电子表格包含命名对象列表、对象的所有者及其所有者的 ClVal。
假设 PeoplePower, INC.(我当场编造的名称)拥有三台计算机。那三台计算机的 ClVal 应该都是 0000_Peop。我需要做的是确保如果这些计算机归 PeoplePower, INC. 所有,则它们具有正确的 ClVal。
我的局限之一是我不能简单地将 ClVal 的子字符串与所有者名称进行比较。这是因为如果我从 ClVal 中取出子字符串 "Peop" 并在所有者字段中查找它,对于名称中某处带有 "Peop" 的任何内容,它将 return 为真(存在)。我想限制任何误报。
为了增加另一层复杂性,只有大量机器的所有者才能获得自己独特的 ClVal。对于其他所有人,他们的 ClVal 为 "other"。但是,有时大公司的机器会被赋予"other" ClVal。
从整体上看,基本上有以下三种情况我们需要检查:
If the computer has the correct ClVal (e.g a PeoplePower computer has the ClVal "0000_Peop"), we should assign that cell's value to 1. This helps operators identify machines with the correct ID at a glance, and allows us to enumerate correctly identified machines.
If the computer has an incorrect ClVal (i.e. a PeoplePower computer has the ClVal "Other"), we should assign that cell's value to 0. This helps operators identify "minor errors" - needs to be fixed but it can wait.
If the computer does not have a ClVal or it has the ClVal of another owner, the cells value should be a -1. This helps operators identify "major errors" that need to be fixed immediately.
到目前为止,我想到了一种方法,但我想知道是否有任何 better/more 有效的选择。我目前有大约 3500 行信息需要排序,而且这个数字还在稳步上升,所以我需要一个能够处理更多行的解决方案。
想法:
string ClVal = Convert.ToString(((Excel.Range)excelStuff.xlWorksheet.Cells[rowIndex, 2]).Value2);
string name = Convert.ToString(((Excel.Range)excelStuff.xlWorksheet.Cells[rowIndex, 5]).Value2);
if (name.Contains("PeoplePower"))
{
string ProperClVal = "0000_Peop";
row[4] = testClVal(ClVal, ProperClVal);
}
//Repeat with else if for all of the major owners
else
{
if (ClVal == "Other")
{
row[4] = 1;
}
else
{
row[4] = -1;
}
}
//while循环外
private int testClVal(String reportedClVal, String ProperClVal)
{
if (reportedClVal == ProperClVal)
{
return 1;
}
else if (reportedClVal == "Other")
{
return 0;
}
else
{
return -1;
}
}
这是功能性的,但它是一堆 if-then 语句,如果我的 excel 电子表格由于错误数据(发生在一些电脑)。
是否有更多 efficient/better/faster 方法来完成此任务?对于 3800 行,它将数据的加载时间table 从 1.5 分钟增加到 2.5 -3 分钟。
怎么样
row[4] = ClVal == "0000_Peop" ? 1 : ClVal == "Other" ? 0 : -1
此外,我还想提一下风格方面的一些问题。
- 请使用
string
,不要到处使用String
。这是 C# 中公认的约定。 - 您的局部变量应该是驼峰式命名并且以小写字母开头。
ProperClVal
,例如,对我来说看起来像一个 class 名字。
这是您可以用于此类任务的最快的算法类别,因为您的程序需要接触每一行。因此,您正在考虑必须优化程序的其他部分或其他模式。
使用复杂性术语 类 (https://en.wikipedia.org/wiki/Complexity_class) 这是一个 O(n) 任务,这意味着 2000 个条目所花费的时间是 1000 个条目的两倍。您想要执行的操作是检查每个值并做出不(非常)依赖于该值有多大或有多少值的决定。在伪代码中:
1) for every Row in Sheet
2) lookup a fixed number of values
3) do a fixed number of comparisons
4) assign a result
5) end loop
第 1 行将被执行 n
次。对于循环中的每次迭代,您将执行一组成本不变的操作;我们会说成本是 1
。这意味着总数 cost/complexity 将是 n
*1
=n
,这就是为什么这是 O(n)。
为了加快速度,我会考虑使用 Excel 互操作以外的东西(它比 NPOI https://npoi.codeplex.com/). Also see if you can refactor the problem: perhaps you don't really need to compute all the values at once but could "lazily-load" them https://en.wikipedia.org/wiki/Lazy_loading 慢,否则会延迟计算
一次从 excel 读取一个值真的很慢。您应该一步读取所有数据,例如:
public object[,] GetArray(int topRow, int rows, int columns)
{
Range c1 = (Range)Worksheet.Cells[topRow + 1, 1];
Range c2 = (Range)Worksheet.Cells[topRow + 1 + rows - 1, columns];
Range range = Worksheet.get_Range(c1, c2);
return range.Value;
}