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 Inan-Evin · Mar 06, 2015 at 04:10 PM · optimizationvariablescpucalculation

Optimized Scripting - Variables

Hello everyone, I have a small question in mind. I am writing a sway script for an object, the object's position will be swayed using Mathf.Sin, according to the Vector3 variable that is adjusted through the inspector. I am going to be giving minor examples, but I will expand this script and use it in a lot of places, that's why I am trying to be careful about optimization and asking this question. Now, what grinds my mind is, if I do not want any sway in for example Z axis, I'll leave it 0 right. But if I write the sway script like this :

 public Vector3 amount;
 
 transform.position = new Vector3(Mathf.Sin(Time.time) * amount.x, Mathf.Sin(Time.time) * amount.y, Mathf.Sin(Time.time) * amount.z);

This script will try to calculate the sin variable for "0", and I think this may hold some space in the memory or cpu right ? So I thought, I can create variables like this:

 float swayX = 0.0f;
 if(amount.x != 0)
 swayX = Mathf.Sin(Time.time) * amount.x;

and same for the other ones. But I do not know if this kind of checking will cause more overload on the cpu than the first option ?

The other way that I think about is :

 private float swayX;
 
 if(amounts.x != 0)
 swayX = Mathf.Sin(Time.time) * amount.x;

This one is similar to the second option, but this will also hold a place in the memory for swayX variable, like the second one does.

I do not know if these three options make a difference, eventhough will be used a lot or not, but I still got doubts in my head, and if someone could clear them, it'd be perfect ! Thanks :).

Comment
Add comment · Show 3
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 hexagonius · Mar 06, 2015 at 04:41 PM 1
Share

Waste of time to think about optimisation here, really

avatar image Inan-Evin · Mar 06, 2015 at 05:13 PM 0
Share

I'm clear now, thanks guys !

avatar image meat5000 ♦ · Mar 06, 2015 at 05:17 PM 0
Share

Indeed, if you need to think about this kind of optimisation you have likely done a lot wrongly, somewhere else.

The way to test this is make a blank project and run the code 10000 times in loop. See which one takes longer.

1 Reply

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

Answer by Glurth · Mar 06, 2015 at 05:12 PM

Zero is a perfectly valid input for Sin. I don't think checking for Zero will optimize it much.

but you COULD optimize this: (EDIT: This will help, not because I'm multiplying the vector by a float, rather than each component, but because Sin function is called only once)

 transform.position = new Vector3(Mathf.Sin(Time.time) * amount.x, Mathf.Sin(Time.time) * amount.y, Mathf.Sin(Time.time) * amount.z);

into

 transform.position = new Vector3(amount.x, amount.y, amount.z) *Mathf.Sin(Time.time);

 

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

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

23 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 avatar image avatar image

Related Questions

How can I get the number of physical cores in the CPU (not logical cores) 1 Answer

Distance Calculation vs OnTriggerStay 2 Answers

Member vs Global variable speed 1 Answer

What is the best CPU for Unity Physics? 2 Answers

Number of scripts per Object. Optimization question. 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