- Home /
Silly question, doing nothing?
Doing allot of dev work and testing out ideas for architecture. One thing I noticed:
Public void DoSomething(){
If(nowDoSomthing == true){
Debug.Log("Now Do it!!!!");
}
}
Ok, so this is basically the question, DoSomething() is running all the time but only doing something when "nowDoSomthing == true", so is "DoSomething()" burning resources (CPU) while its actually doing nothing?
Answer by Eric5h5 · Aug 23, 2013 at 08:40 PM
Yes, there is overhead in calling a function. It's not much, but it's non-zero. The "if" check is not free either. Ideally you would not call functions if you don't need to, but depending on what you're doing it's probably not worth worrying about. An example of something to worry about would be a tight loop repeated millions of times...in that case you'd be better off removing function calls where possible. As far as I know, the version of Mono in Unity does not inline functions.
The only time code is not serialised is with threading, no?
@meat5000: I'm not sure what you're talking about regarding serialised and threading.
@superme2012: no, just a demigod. ;)
By serialisation I mean that on compile all the code is put into the $$anonymous$$ain function so it can be processed by the cpu in a serial fashion. When something is threaded its like working in parallel (serialised parallel). When you write a function the compiler takes the function and basically inserts the contents into $$anonymous$$ain(), right? This is why I don't see why functions have an overhead.
EDIT: I stand corrected
Useless here :D I write AS$$anonymous$$ for firmware and DoNothing is well important for getting your ti$$anonymous$$gs correct.
Padding basically :D
Answer by meat5000 · Aug 23, 2013 at 08:34 PM
First you must call
DoSomething()
e.g from your Update()
Once it is called the if statement inside it checks if the Variable
nowDoSomthing == true
if it is youll get whats in the if statement, which will throw an error in this case as you've misspelled Debug :P
Place DoSomething() in Update() and correct Debug and you will get your output.
If you don't call DoSomething() it will do nothing
Yeah I sort of know that, but that’s not what I asked?
is DoSomething() burning resource while nowDoSomthing != to true?
Nah its just an empty container (nearly). If you had a thousand of them it might make an impact but as Xtro says its the least of your worries and wont make a dent if you are trying to optimise
There is a branch in there (if) which takes 2 routines compared to a simple variable=Something's 1 routine but really CPUs these days would blow it off like dust
An accurate answer is : Yes it uses CPU cycles to do nothing
@meat5000: On compile, the function is not written into the $$anonymous$$ain as if the code was in $$anonymous$$ain to begin with...
IIRC, In compiled languages, especially in assembly, "some times" this would be true if you used a short jump pointer, and your function definition and call were in the same segment. Thats what in-lining does. Generally though, by and large, your code pushes your parameters onto the stack and performs a "jump" in execution to the new location, with a "ret" value pushed onto the stack as well.
In short, there is an overhead for a function call, though really tiny. For interpreted languages, there may be an ever higher overhead, and for OOP languages, even more so.
So is it equivalent to 2 branches? Push, jump, pop, jump?
Answer by Xtro · Aug 23, 2013 at 08:37 PM
Calling a method doesn't burn CPU. There are millions of other things that can burn the CPU but just calling a method is the least of them. Don't worry.
Answer by wiiarethesound · Aug 23, 2013 at 08:38 PM
"Burning" resources might be a bit of an overstatement. If you're really worried about it, make your check before you even call the function:
if (nowDoSomething)
{
DoSomething();
}
This will prevent the function from being called unnecessarily, but honestly the amount of savings will be negligible. Most modern processors will hardly even notice it.
@meat5000: It won't be the same, since if you don't do the check first, it has to call the function, which does have overhead.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
What is the best way to learn Unity3D? 6 Answers
Destroying Elements From An Array 1 Answer
What is the technical name of component-based approach in Unity? 1 Answer