Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by superme2012 · Aug 23, 2013 at 08:30 PM · programmingarchitecture

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Aug 23, 2013 at 08:58 PM 0
Share

The only time code is not serialised is with threading, no?

avatar image superme2012 · Aug 23, 2013 at 09:22 PM 0
Share

Eric is the god of unity ;)

avatar image Eric5h5 · Aug 23, 2013 at 09:29 PM 0
Share

@meat5000: I'm not sure what you're talking about regarding serialised and threading.

@superme2012: no, just a demigod. ;)

avatar image meat5000 ♦ · Aug 23, 2013 at 09:38 PM 0
Share

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

avatar image meat5000 ♦ · Aug 23, 2013 at 10:31 PM 1
Share

Useless here :D I write AS$$anonymous$$ for firmware and DoNothing is well important for getting your ti$$anonymous$$gs correct.

Padding basically :D

Show more comments
avatar image
0

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superme2012 · Aug 23, 2013 at 08:37 PM 0
Share

Yeah I sort of know that, but that’s not what I asked?

avatar image superme2012 · Aug 23, 2013 at 08:39 PM 0
Share

is DoSomething() burning resource while nowDoSomthing != to true?

avatar image meat5000 ♦ · Aug 23, 2013 at 08:47 PM 0
Share

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

avatar image Sajidfarooq · Aug 23, 2013 at 09:41 PM 1
Share

@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.

avatar image meat5000 ♦ · Aug 23, 2013 at 09:48 PM 0
Share

So is it equivalent to 2 branches? Push, jump, pop, jump?

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 23, 2013 at 09:18 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges