patterncsharpModerate
Any performance difficulties related to List<Type>?
Viewed 0 times
anyrelatedtypeperformancelistdifficulties
Problem
I'm using the following structure:
And I'm iterating this
There is around 500*500 points
This iterating is calling
And my application can be initiated around 30 seconds.
Shall I use Array instead of
Shall I use
Or, what should I do?
class Map
{
public List> points;
public List bases;
}Point and Base are over-normal sized classes (has more than 30 attributes).And I'm iterating this
Map class like this:public static void calculateMatrix(Map map)
{
LineOfSight.LineOfSight los = new LineOfSight.LineOfSight();
foreach (var b in map.bases)
{
foreach (var cc in map.points)
{
foreach (var c in cc)
{
calculatePointValues(c, b, los);
}
}
}
}There is around 500*500 points
List> and around 10 bases.This iterating is calling
calculatePointValues method around 2.5 million.And my application can be initiated around 30 seconds.
Shall I use Array instead of
List<>?Shall I use
for() instead of foreach?Or, what should I do?
Solution
As was said in the comments, I don't think
But with a bit of LINQ you can reduce the nesting a bit:
And it could be worth trying to profile it with some
List makes any significant difference over an array.But with a bit of LINQ you can reduce the nesting a bit:
foreach (var b in map.bases)
{
foreach (var c in map.points.SelectMany(cc => cc))
{
CalculatePointValues(c, b, los);
}
}And it could be worth trying to profile it with some
Parallel looping as well:Parallel.ForEach(map.bases, b =>
{
foreach (var c in map.points.SelectMany(cc => cc))
{
CalculatePointValues(c, b, los);
}
});Code Snippets
foreach (var b in map.bases)
{
foreach (var c in map.points.SelectMany(cc => cc))
{
CalculatePointValues(c, b, los);
}
}Parallel.ForEach(map.bases, b =>
{
foreach (var c in map.points.SelectMany(cc => cc))
{
CalculatePointValues(c, b, los);
}
});Context
StackExchange Code Review Q#48150, answer score: 11
Revisions (0)
No revisions yet.