Need some experienced advice on performance issues of global variables and calling functions byv
So far my application performance is great and there are no bugs so to speak....however, I as a noob, I want to make sure that I am writing proper code and not making any obvious mistakes that could hurt performance or optimization. I am doing llots of vector calculations and drawing lines on several panels on the fly, and most of the data has to be global so the lifetime is there for future calculations. Is this a bad thing to have so many globals? Also, many of the variables share the same function. Is it faster performance wise to call the function and pass the variable to it, even though it is not really necessary as the function can already "see" the global var? It does make readability much better when writing code. Should I try to Dim more variables privately and pass values byref? There are so many different options I am starting to get a little intimidated. My main concern here is application speed and tidyness of the code. It would seem that allocating memory beforehand(global var) would eliminate what has to happen "on the fly" but increase memory consumption somewhat. Any insight to all of this would be so helpful!
Also, is this acceptable syntax from say form2 or a module?
value=form1.textbox.text
form1.namebox.text=textbox1.text.
"Is it faster performance wise to call the function and pass the variable to it?
Any time you pass an argument, it's slower but it's better practice to so. If you don't as your complexity increases, your confusion will go up at a high rate and your code is going to become increasingly difficult to maintain and modify.
"Should I try to Dim more variables privately and pass values byref?"
You only need to pass them byref if you know you are going to modify them in the called method. Your propose method provides better control and less bugs. By ref is slower than byval.
"It would seem that allocating memory beforehand(global var) would eliminate what has to happen "on the fly" but increase memory consumption somewhat. Any insight to all of this would be so helpful!"
This is always a tradeoff in software engineering .....to a point. It's faster to work in memory when it's truly available. Taking this to an extreme invites paging an them you have a disk between you and memory.
Hi,
You can pass variables around as GLOBAL if they are Public within a MODULE.>>>>
Code Snippet
Module
Module1 'GLOBAL Public variables like these can be ' "seen" by all FORMs. :-) ' Hope this helps you. 'E.G. Public myInteger As Integer Public myString As String '..... and so on. End
Module Form1 code.>>
Code Snippet
Public
Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myInteger = 7
Dim aForm As Form = Form2 aForm.Show()
End Sub End
Class Form2 code.>>
Code Snippet
Public
Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show(myInteger.ToString)
End Sub End
Class I hope this helps you.
Regards,
S_DS
Which has what to do with the OP's original question?
I just wanted to say that i understand why reneec asked the validity of SD's response to the question. But speaking from a beginner's point of view when i see questions that are technical and then i see example code, it seems to make more sense even if it doesn't really answer the question. It gives a face to the name.
I think the people that will benefit from this thread with the code samples added in will be the beginners just starting out. I have a good understanding of the original question but i can see many who will have no idea what it means without seeing some code to reference. I think we beginners think quite a bit differently than all of you with so much knowledge. (reneec, spotty, nobugz, solitaire, sd, derek smith, etc...) Were just trying to make sense of it all.
Sorry for butting in i just thought it might ease the tension.
Thanks,
I guess after thinking about it, I should have worded my question more carefully. The goal here is a smooth seamless framerate as this is a visual application. It is getting to where I am crunching so much math in one frame (and drawing all of that data on 4 seperate panels at 20 fps or so) I want to make sure that there is specifically something I should avoid that would hurt performance.
Coming from learning on 8 bit microprocesors, I think I am underestimating the sheer power of the pc. At this point I am completely happy with performance at this point, but there is always room for improvement!
If you put stopwatches on your code, you'll probably find that the math is inconsequential. All the time will be spent by the OS drawing your panels.
ReneeC wrote: |
| Which has what to do with the OP's original question? | |
Hi,
I was giving an example of a way of declaring GLOBAL variables that are GLOBAL to all forms and not just Global to all SUBroutines and Functions at FORM level.
ReneeC , i do wish that;
a) You would stop criticising posts from other forum users such as myself and
b) Read every word of EVERY bit of an OP question, including the title.
I could tell that with the OP putting "noob" and having less than 20 posts that an OP is a beginner, particularly if they state the FACT.
The OP mentioned GLOBAL variables, has marked my post as an answer too.
So honestly, have you something against me, or are you like that all the time?
By the way, I HATE those who criticise ALL THE TIME.
Regards,
S_DS
js06 wrote: |
| I just wanted to say that i understand why reneec asked the validity of SD's response to the question. But speaking from a beginner's point of view when i see questions that are technical and then i see example code, it seems to make more sense even if it doesn't really answer the question. It gives a face to the name. I think the people that will benefit from this thread with the code samples added in will be the beginners just starting out. I have a good understanding of the original question but i can see many who will have no idea what it means without seeing some code to reference. I think we beginners think quite a bit differently than all of you with so much knowledge. (reneec, spotty, nobugz, solitaire, sd, derek smith, etc...) Were just trying to make sense of it all. Sorry for butting in i just thought it might ease the tension. | |
Hi js06,
I wish all forum users were as like-minded as yourself, easier-going and not so critical as ReneeC is.
I think She has something against me, to be honest, or that She wants to be "Queen of the Microsoft forums". 
Regards,
S_DS