Cags
Contributor
I've written a small section of code that takes alot longer to complete than I think it should. The code is actually using a Graphics object but I put this post in the General forum as I believe the bottleneck has nothing todo with the actuall drawing.
The following code takes approximately 480ms to complete (whereas simply drawing the equivalent number of rectangles takes about 9ms).
Given....
Class square is a small class with properties for Bounds, LinkType and SquareType. I was originally using a nested for loop to cycle through each square, but found that using a foreach loop was slightly quicker. I'm wondering if using Square[,] is not a good method of creating an array, maybe I should build a custom collection by inheriting collectionbase.
Has anybody got any ideas on the best way of speeding up this method? (as my application is relatively simple I can always get around it by drawing only parts of it at a time, I just thought I'd check to see if anyone notices an obvious bottleneck first)
The following code takes approximately 480ms to complete (whereas simply drawing the equivalent number of rectangles takes about 9ms).
C#:
foreach(Square sqTemp in arrGrid)
{
if(sqTemp.Type == SquareType.Node)
gBuffer.FillRectangle(Brushes.Black, sqTemp.Bounds);
else if(sqTemp.Type == SquareType.Link)
{
if(sqTemp.Link == LinkType.Yes)
{
gBuffer.FillRectangle(Brushes.Black, sqTemp.Bounds);
}
else if(sqTemp.Link == LinkType.No)
{
gBuffer.FillRectangle(Brushes.Red, sqTemp.Bounds);
}
}
}
Given....
C#:
Square[,] arrGrid = new Square[31,51];
public enum SquareType
{
Node, Link, Number
}
public enum LinkType
{
Unknown, Yes, No
}
Class square is a small class with properties for Bounds, LinkType and SquareType. I was originally using a nested for loop to cycle through each square, but found that using a foreach loop was slightly quicker. I'm wondering if using Square[,] is not a good method of creating an array, maybe I should build a custom collection by inheriting collectionbase.
Has anybody got any ideas on the best way of speeding up this method? (as my application is relatively simple I can always get around it by drawing only parts of it at a time, I just thought I'd check to see if anyone notices an obvious bottleneck first)