Question about optimisation

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

What do you think about this approaches, wich one is the best?

For x As Integer = u.GetViewBounds.X To u.GetViewBounds.X +
u.GetViewBounds.Width
For y As Integer = u.GetViewBounds.Y To
u.GetViewBounds.Y + u.GetViewBounds.Height
Dim Dx As Integer = x - u.Location.X
Dim Dy As Integer = y - u.Location.Y
If Dx * Dx + Dy * Dy <= u.ViewRange * u.ViewRange
Then 'punctul e in cercul vizual
AddCorrectPoint(x, y)
End If
Next
Next

or


dim vr as rectangle=u.GetViewBounds
For x As Integer = vr.X To vr.X + vr.Width
For y As Integer = vr.Y To vr.Y + vr.Height
Dim Dx As Integer = x - u.Location.X
Dim Dy As Integer = y - u.Location.Y
If Dx * Dx + Dy * Dy <= u.ViewRange * u.ViewRange
Then 'punctul e in cercul vizual
AddCorrectPoint(x, y)
End If
Next
Next

Should I declare a variable as rectangle?
I'm not sure how compiler will optimize this and a call to u.GetViewBounds
each time is looping is a speed problem to me
 
Crirus,

IMHO, the second way will be faster because you only execute "GetViewBounds"
once.

You could always use the "With" clause to achieve the same effect.

Generally if you have to do something like:

a.b.z = a.b.x + a.b.y

Then it would be more efficient to use:

With a.b

.z = .x + .y

End With

The advantage is even greater if a.b is a function that takes time to
execute.

The VB compiler probably does some optimizations with fields as opposed to
properties and methods.

Hope this helps,

Trev.
 
Crirus,
dim vr as rectangle=u.GetViewBounds
Should I declare a variable as rectangle?
I don't think that is going to make a worthwhile change to the function.

Instead of Rectangle.X + Rectangle.Width, I would use Rectangle.Right. (or
Rectangle.Bottom), however I don't think this will make a worth while change
either (IMHO it makes it more readable).
dim vr as rectangle=u.GetViewBounds
For x As Integer = vr.X To vr.X + vr.Width
For x As Integer = vr.Left to vr.Right

Remember the vr.Left is the same as vr.X. Again readability not
performance...


However you should be able to reduce this to some rather interesting
formulas. Coming up with these formulas (this algorithm) I think is where
you are going to gain your performance boost!

From the code it appears you "simply" want the intersection of a circle with
a Rectangle? Correct? For the points that interest you want to call
AddCorrectPoint? Correct?

Searching google for "intersection circle rectangle" returns some
interesting and promising results...

Off hand:
for each row
find min col intersecting circle, limit to bounds
find max col intersecting circle, limit to bounds
for min col to max col
AddCorrectPoint

The intersection of a line with a circle should be a "simple formula"...

http://mathworld.wolfram.com/Circle-LineIntersection.html

Its been too long since I've had geometry ;-)

Hope this helps
Jay
 
Well, actualy, I simply need points inside the circle.. the rectangleis the
bounding one already :)
I was just wandering if the call to the property is better than allocating
temporary variables to hold properties values
 
I was just wandering if the call to the property
is better than allocating temporary variables to
hold properties values

IMHO, it depends on how the property is implemented. If it is a simple field
retrieval, then the compiler should optimise it so speed should be pretty
much the same as if you assigned it to a local variable. However, if the
property get procedure does some calculation to produce the value, then
assigning to a local variable should have a definite speed advantage.

If in doubt, always assign to a local variable or use a "with" block.

HTH,

Trev.
 
Crirus,
Well, actualy, I simply need points inside the circle.. the rectangleis the
bounding one already :)
Which is where I still recommend finding a new algorithm instead of tweaking
the existing algorithm. By tweaking I mean calling a property or allocating
temporary variables, as the optimizer can do that for you behind the
scenes...

I'm recommending finding a new engine to course tune the car, once you have
a good engine, then worry about fine tuning it. I realize sometimes you need
to go with the engine you have ;-)
I was just wandering if the call to the property is better than allocating
temporary variables to hold properties values
To truely answer this question you will need to profile it! Run what you
have through a profiler, change it run the change thru a profiler. Compare
the results. As Codemonkey stated, for simple fields, the optimizer should
inline the property method. However there are too many factors involved to
give you a general answer, as the general answer could change based on other
code...

Using "Rectangle.X + Rectangle.Width" instead of "Rectangle.Right" is
something you should also profile, the optimizer might move "Rectangle.X +
Rectangle.Width" outside the loop as the value does not change inside the
loop. I don't remember if the JIT does this optimization or not.


The following articles provide information on writing .NET code that
performs well.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/fastmanagedcode.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/vbnstrcatn.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchperfopt.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftechs.asp

Hope this helps
Jay
 
Back
Top