Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimizing Looping Procedure

Status
Not open for further replies.

MrCSharp

IS-IT--Management
Jun 7, 2005
59
CA
I have a Method that generates random coordinates to position rectangles, no rectangles can intersect with each other. Works but I need to speed up the performance, I am hoping someone could give me an idea.

private void RandomCords()
{
DateTime StartTime = DateTime.Now;
PositionCoords.Clear();
Random R = new Random();
ArrayList rectList = new ArrayList();
for (int i = 0; i < 10; i++)
{
ScreenCoord coord = new ScreenCoord();
coord.X = R.Next(Enviro1.X_Constraint, Enviro1.ScreenWidth - Global.imageWidth);
coord.Y = R.Next(Enviro1.Y_Constraint, Enviro1.ScreenHeight - Global.imageHeight);
System.Drawing.Rectangle r1 = new System.Drawing.Rectangle(coord.X, coord.Y, Global.imageWidth, Global.imageHeight);

int k = 0;
while (k < rectList.Count)
{
if (r1.IntersectsWith(((System.Drawing.Rectangle)rectList[k])))
{
coord.X = R.Next(Enviro1.X_Constraint, Enviro1.ScreenWidth - Global.imageWidth);
coord.Y = R.Next(Enviro1.Y_Constraint, Enviro1.ScreenHeight - Global.imageHeight);
r1.X = coord.X;
r1.Y = coord.Y;
k = 0;
}
else
k++;
}
PositionCoords.Add(coord);
System.Drawing.Rectangle tempRect = new System.Drawing.Rectangle(coord.X, coord.Y, Global.imageWidth, Global.imageHeight);
rectList.Add(tempRect);
}
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
Response.Write("Page Generated In: " + ts.TotalMilliseconds.ToString() + " ms");
 
one simple way to speed up loops is to eliminate using the new keyword within them.

Instead of instantiating a new object within each iteration, initialize it before the loop, and reuse it each iteration. This will prevent .Net from having to add it to the heap each time, and then destroying it, a costly process. I've seen this method speed up large loops even when using something as simple as a single integer.

I know this is a generic optimization and may not work in all cases, but its a start. I'll look more at wat your trying to do and see if I can offer any more suggestions.
 
check out the flyweight pattern. most examples use printing font/size letters to the screen, but the same would be true of rectangles. I haven't used this pattern, but I have come across it when researching OOP

I would also use a generic list instead of an arraylist.
IList<Retangle> listOfRectangles = new List<Retangle>();
I find
foreach(Rectangle rectangle in listOfRectangles)
{
//do work
}
much easier to read than
int index = 0;
while(index < listOfRectangles.Count)
{
Rectangle rectangle = (Rectangle)listOfRectangles;
}


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
ArrayList should not be used as it requires boxing see jmeckley List<>
Code:
 ArrayList rectList = new ArrayList();// :(
IList<Retangle> listOfRectangles = new List<Retangle>();
//:)

Move Var declarations out of the loop and then just re-use the var in the loop.
Code:
int k = 0;

I think that these could be calculated out of the loop instead of recalculating on every loop as they are just used for random numbers?
Code:
Enviro1.ScreenWidth - Global.imageWidth // .. etc

Dont use magic numbers, use constants or vars.
Code:
for (int i = 0; i < 10; i++) // 10 is magic
The foreach loop is slower than a for loop

my 2 cents


Age is a consequence of experience
 
Thanks for the suggestions I made some of those changes and it definitley sped it up, I was also looking for a way to eliminate generating unuseable coordiantes over again. Can you give any suggestions on this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top