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
0
Question by BruceT · Dec 18, 2012 at 03:55 AM · performancefunction

One line function and performance.

Hi guys,

For the last week I have been reading about optimization and it has made a huge difference to the speed of my zombie game. One of the things I read was about not repeating lines.

I have a line of code that I use about 4 times in my script that checks distance to the player. So I put the line of code in a function...

 function DistanceToPlayer()
 {
     distanceToPlayer = ( playerTransform.position - zombieTransform.position ).sqrMagnitude;
 }

It struck me though that to call a function with one line of code would be less optimal than simply using the line of code where needed. Two operations have to be done instead of one, a call to the function and then execution of the line.

It might be a pedantic question but is it better to simply use the one line of code where needed or put it in a function? Or doesn't it make any difference except for cleaner code?

Many thanks,

Bruce

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

1 Reply

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

Answer by Eric5h5 · Dec 18, 2012 at 04:03 AM

It's better to put it in a function, since DistanceToPlayer is more readable than the line of code, and if you change the way it works for whatever reason, you only need to change the code in the function instead of having to change it everywhere. It's true that there is overhead for calling a function, but it's not likely to make any difference unless you're doing it a really large number of times (say, thousands of time in a loop that runs every frame).

There's also a compiler feature known as "function inlining", where even though you write code using functions, the compiler goes and basically replaces function calls with the line of code for you. That way you get the readability and maintenance benefits of writing code using functions, but the performance of using the code directly. The only issue is that Unity doesn't currently actually do function inlining as far as I know, but that could change the next time they update the version of Mono that Unity uses.

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 BruceT · Dec 18, 2012 at 04:17 AM 0
Share

Thanks Eric! I had not even considered the changing of the code until you mentioned it, I had been going through and changing every line of code.

Very interesting what you said about the function inlining too. Understanding that will void me needing to ask silly questions again. Hopefully they will implement it as you say. Thanks again :)

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

10 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

Related Questions

Which Unity functions are generally slow/bad for performance? 4 Answers

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

How do I create a function that accepts EITHER vector3 OR three floats? 1 Answer

Using a member variable from one function in another 3 Answers

How to use different types of scripts with an override function. 2 Answers


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