473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Question about optimisation

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
5 1072
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
how well is javac from sun's jdk tools optimized? wow, that's a general question... so i guess here is a simple example... let's say i have a program that uses an array. and many times in the...
83
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
6
by: Marc Pelletier | last post by:
Hello, Are nested function calls inefficient (or inherently evil)? In Delphi I've seen small but significant performance improvements by always declaring a local variable and 'unwrapping' the...
32
by: Axel Bock | last post by:
Hi all, I am trying to get my head around what happens if I return a class object from a function. It seems C++ (MinGW) does not invoke the copy constructor if I do something like this: ...
45
by: Robbie Hatley | last post by:
Hello, group. I've been doing too much C++ programming lately, and I'm starting to become rusty at some aspects of the C way of doing things, esp. efficient low-level data copies. ...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
12
by: Avalon1178 | last post by:
Hi, I have an application that periodically uses a std::string variable which is assigned a VERY VERY large string (15000000+ bytes long). This application is essentially a daemon, and it polls...
5
by: runa2104 | last post by:
Interview Question Visit this site it is really good question on C language. http://techinterviewquestion.blogspot.com/2007/05/c-question.html
31
by: Dave S | last post by:
Hi All, I have been given some code to wok on. It relies heavily on the optimiser to run at the correct speed. With this in mind I have been loking through it to see if I can help it out a bit. I...
26
by: Herbert Kleebauer | last post by:
As part of a simple X demo for Linux I send the byte string in "send8" to the X server. This byte string displays a circle at the x/y position send8.s8x/send8.s8y. In "display()" a 8x8 grid of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.