C# 这就是说,对于一些简单的优化,有一个小小的改变。因为在本例中,我们只处理1和0,因此我们可以使用编译器进行的内部优化,从而为我们带来真正的好处。与其检查一个值是否为0,实际上只需将其添加到我们的运行总和中,速度要快得多 static int GetMaxI
C# 这就是说,对于一些简单的优化,有一个小小的改变。因为在本例中,我们只处理1和0,因此我们可以使用编译器进行的内部优化,从而为我们带来真正的好处。与其检查一个值是否为0,实际上只需将其添加到我们的运行总和中,速度要快得多 static int GetMaxI,c#,arrays,linq,plinq,C#,Arrays,Linq,Plinq,这就是说,对于一些简单的优化,有一个小小的改变。因为在本例中,我们只处理1和0,因此我们可以使用编译器进行的内部优化,从而为我们带来真正的好处。与其检查一个值是否为0,实际上只需将其添加到我们的运行总和中,速度要快得多 static int GetMaxIndex_EvenBetter(byte[,] TwoDArray) { int NumRows = TwoDArray.GetLength(0); int NumCols = TwoDArray.Get
这就是说,对于一些简单的优化,有一个小小的改变。因为在本例中,我们只处理1和0,因此我们可以使用编译器进行的内部优化,从而为我们带来真正的好处。与其检查一个值是否为0,实际上只需将其添加到我们的运行总和中,速度要快得多
static int GetMaxIndex_EvenBetter(byte[,] TwoDArray)
{
int NumRows = TwoDArray.GetLength(0);
int NumCols = TwoDArray.GetLength(1);
int RowCount, MaxRowCount = 0, MaxRowIndex = 0;
for (int row = 0; row < NumRows; row++)
{
RowCount = 0;
for (int col = 0; col < NumCols; col++)
{
RowCount += TwoDArray[row, col]; //See my change here
}
if (RowCount > MaxRowCount)
{
MaxRowCount = RowCount;
MaxRowIndex = row;
}
}
return MaxRowIndex;
}
static int GetMaxIndex\u甚至更好(字节[,]TwoDArray)
{
int NumRows=TwoDArray.GetLength(0);
int NumCols=TwoDArray.GetLength(1);
int RowCount,MaxRowCount=0,MaxRowIndex=0;
for(int行=0;行最大行计数)
{
MaxRowCount=行计数;
MaxRowIndex=行;
}
}
返回MaxRowIndex;
}
在大多数其他情况下,您不只是使用1和0,因此您确实希望在添加之前检查这些值,但在这里,这是不必要的。
它需要是字节[,]
还是IEnumerable
或字节[]
?@Los Firjoles我可能可以将其设置为IEnumerable或字节[][]的锯齿数组。我想您已经看到了类似的答案。。。什么建议不起作用/为什么?@Alexei Levenkov我确实看到了第一个链接,但不确定是否看到了第二个链接。其他问题/解决方案没有任何分组代码,我还不是LINQ专家。在发布之前花了一些时间做研究,并试图找出它如何与LINQ合作。非常感谢下面的解决方案。我现在想做的是在上面的循环代码中添加一些并行代码,并将其与下面的每个解决方案进行比较,看看哪一个最快。当我有更多的时间时,我会在几天后用结果更新OP。
var arr = new [,] { { 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 0 }, { 0, 0, 1, 1, 1 }, { 1, 0, 1, 0, 1 } };
var data =
Enumerable.Range(0, 4)
.Select(
row =>
new
{
index = row,
count = Enumerable.Range(0, 5).Select(col => arr[row, col]).Count(x => x == 1)
})
.OrderByDescending(x => x.count)
.Select(x => x.index)
.First();
// This code is extracted from
// http://www.codeproject.com/Articles/170662/Using-LINQ-and-Extension-Methods-in-C-to-Sort-Vect
private static IEnumerable<T[]> ConvertToSingleDimension<T>(T[,] source)
{
T[] arRow;
for (int row = 0; row < source.GetLength(0); ++row)
{
arRow = new T[source.GetLength(1)];
for (int col = 0; col < source.GetLength(1); ++col)
arRow[col] = source[row, col];
yield return arRow;
}
}
// Convert byte[,] to anonymous type {int index, IEnumerable<byte[]>} for linq operation
var result = (from item in ConvertToSingleDimension(Array2D).Select((i, index) => new {Values = i, Index = index})
orderby item.Values.Sum(i => i) descending, item.Index
select item.Index).FirstOrDefault();
private static int GetMaxIndex(byte[,] TwoDArray) {
return Enumerable.Range(0, TwoDArray.GetLength(0))
.Select(
x => new {
Index = x,
Count = Enumerable.Range(0, TwoDArray.GetLength(1)).Count(y => TwoDArray[x, y] == 1)
})
.OrderByDescending(x => x.Count)
.First()
.Index;
}
private static int GetMaxIndex2(byte[,] TwoDArray) {
return ParallelEnumerable.Range(0, TwoDArray.GetLength(0))
.Select(
x => new {
Index = x,
Count = Enumerable.Range(0, TwoDArray.GetLength(1)).Count(y => TwoDArray[x, y] == 1)
})
.OrderByDescending(x => x.Count)
.First()
.Index;
}
//arry is your 2d byte array (byte[,] arry)
var maxIndex = arry
.Cast<byte>() //cast the entire array into bytes
.AsParallel() //make the transition to PLINQ (remove this to not use it)
.Select((b, i) => new // create indexes
{
value = b,
index = i
})
.GroupBy(g => g.index / arry.GetLength(1)) // group it by rows
.Select((g, i) => new
{
sum = g.Select(g2 => (int)g2.value).Sum(), //sum each row
index = i
})
.OrderByDescending(g => g.sum) //max by sum
.Select(g => g.index) //grab the index
.First(); //this should be the highest index
public static T[][] GetJagged<T>(this T[,] raw)
{
int lenX = raw.GetLength(0);
int lenY = raw.GetLength(1);
T[][] jagged = new T[lenX][];
for (int x = 0; x < lenX; x++)
{
jagged[x] = new T[lenY];
for (int y = 0; y < lenY; y++)
{
jagged[x][y] = raw[x, y];
}
}
return jagged;
}
static int GetMaxIndexLINQ(byte[,] TwoDArray)
{
byte[][] jagged = TwoDArray.GetJagged();
IEnumerable<int> rowSums = from bitRows in jagged
select bitRows.Sum((b) => b);
int maxIndex = rowSums.Max();
int MaxRowIndex = Array.IndexOf(rowSums.ToArray(), maxIndex);
return MaxRowIndex;
}
static int GetMaxIndex_EvenBetter(byte[,] TwoDArray)
{
int NumRows = TwoDArray.GetLength(0);
int NumCols = TwoDArray.GetLength(1);
int RowCount, MaxRowCount = 0, MaxRowIndex = 0;
for (int row = 0; row < NumRows; row++)
{
RowCount = 0;
for (int col = 0; col < NumCols; col++)
{
RowCount += TwoDArray[row, col]; //See my change here
}
if (RowCount > MaxRowCount)
{
MaxRowCount = RowCount;
MaxRowIndex = row;
}
}
return MaxRowIndex;
}