473,698 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Get ViewBounds
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 1087
Crirus,

IMHO, the second way will be faster because you only execute "GetViewBou nds"
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****@datagro up.ro> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.Get ViewBounds
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.Get ViewBounds
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.Botto m), however I don't think this will make a worth while change
either (IMHO it makes it more readable).
dim vr as rectangle=u.Get ViewBounds
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 "intersecti on 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****@datagro up.ro> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.Get ViewBounds
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******** *****@tk2msftng p13.phx.gbl...
Crirus,
dim vr as rectangle=u.Get ViewBounds
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.Botto m), however I don't think this will make a worth while

change either (IMHO it makes it more readable).
dim vr as rectangle=u.Get ViewBounds
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 "intersecti on 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****@datagro up.ro> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.Get ViewBounds
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******** ******@TK2MSFTN GP09.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******** *****@tk2msftng p13.phx.gbl...
Crirus,
dim vr as rectangle=u.Get ViewBounds
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.Botto m), however I don't think this will make a worth while

change
either (IMHO it makes it more readable).
dim vr as rectangle=u.Get ViewBounds
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 "intersecti on 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****@datagro up.ro> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.Get ViewBounds
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.Righ t" 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******** ******@TK2MSFTN GP09.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******** *****@tk2msftng p13.phx.gbl...
Crirus,
dim vr as rectangle=u.Get ViewBounds
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.Botto m), however I don't think this will make a worth while

change
either (IMHO it makes it more readable).
dim vr as rectangle=u.Get ViewBounds
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 "intersecti on 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****@datagro up.ro> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.Get ViewBounds
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
1664
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 program i need to do computation that rely on the array's length. so, how much speedup (if any at all) is there to storing the array length as an int and using that in the computations, compared with constantly using the arrayName.length method?
83
3027
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
6
1537
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 nested calls, but I'm not convinced it is necessarily so in c#. So for example: public struct TideValue { public TideValue( double Time, double Height ) {
32
2199
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: SomeObj foo() { SomeObj X;
45
4099
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. Specificially, I just wrote the following, but I don't know if this is safe: void Func(char* left, char* right) { chat temp_left = {'\0'};
14
2145
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 and holds everything a unit in my game is supposed to do. What are some some cures for this kind of large object or are they OK because they represent one thing. If not what are better ways to design objects that behave the same way. Would it be...
12
4285
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 a data set which can have a lot of information and it is concatenated in this single string variable object. When the daemon finishes its job, it goes to sleep, but before doing so, it "clears" this variable so it can be reused again in the next...
5
6337
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
1775
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 have limitied knowledge of how compilers works, but I understand that some constructs optimise easier / better. We are using a variant of gcc (3.4.1 I think) on Nios II platform, if that makes a difference. The question I have is are these 2...
26
1949
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 circles is displayed. But when I compile the code below with -O3, then in the "j" loop not send8.s8x is incremented by 50 (send8.s8x += 50;) but only a local copy of it. As a result, the byte string sent to the X server (x_send((char*)&send8,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7725
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.