By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

Question about optimisation

P: n/a
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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #2

P: n/a
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-...ersection.html

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

Hope this helps
Jay

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #3

P: n/a
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uV*************@tk2msftngp13.phx.gbl...
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-...ersection.html

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

Hope this helps
Jay

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------


Nov 20 '05 #4

P: n/a
> 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" <Cr****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP09.phx.gbl... 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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uV*************@tk2msftngp13.phx.gbl...
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-...ersection.html

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

Hope this helps
Jay

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------



Nov 20 '05 #5

P: n/a
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/de...anagedcode.asp

http://msdn.microsoft.com/library/de...anagedapps.asp

http://msdn.microsoft.com/library/de...vbnstrcatn.asp

http://msdn.microsoft.com/library/de...tchperfopt.asp

http://msdn.microsoft.com/library/de...tperftechs.asp

Hope this helps
Jay
"Crirus" <Cr****@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP09.phx.gbl... 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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uV*************@tk2msftngp13.phx.gbl...
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-...ersection.html

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

Hope this helps
Jay

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------



Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.