Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 manny003 · Aug 21, 2015 at 03:23 PM · javascriptperformance optimization

Code Performance: When to use "new" on Vector3

I am calculating a vector normal and storing the results into another vector and it seems to work regardless of whether or not my destination vector is an instance of vector3 or just a variable of type vector3.

Can someone explain what's going on when I need to use "new Vector3()" and when not to?

 var vectorNormalized : Vector3;

 //normalization below seems to work with or without this line.
 vectorNormalized = new Vector3();

 vectorNormalized = Vector3.Normalize(myOriginalVector);
 

In UnityScript/Javascript, what's the most optimal way to do what I'm trying to do?

Thanks, Manny

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by YoungDeveloper · Aug 21, 2015 at 03:34 PM

 var vectorNormalized : Vector3;      //you create new vector 0,0,0
 vectorNormalized = new Vector3();    //you create new vector 0,0,0 again
 vectorNormalized = Vector3.Normalize(myOriginalVector); //new vector is created as normalized from same vector

Dont create useless objects, instead you could simply.

 function Start(){
     var vectorNormalized:Vector3 = new Vector3();
     vectorNormalized.Normalize();
 }
Comment
Add comment · Show 2 · 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 manny003 · Aug 21, 2015 at 03:48 PM 0
Share

Thanks for your quick reply. I have a need to keep both by original vector as well as obtain a normalized version. Would you recommend:

 var vectorNormalized : Vector3;
 
 function someFunctionThatGetsCalled$$anonymous$$ultipleTimes() {
      vectorNormalized = myOriginalVector;
      vectorNormalized.Normalize();
      ....
      ....
  }

Thanks.

avatar image YoungDeveloper · Aug 21, 2015 at 03:54 PM 0
Share

If you need to keep both, then creating a temp cache is the only way.

 private var myVector : Vector3 = new Vector3(5f,5f,5f);
 
 function Funk(){
     var myVectorNormalized:Vector3 = myVector.normalized;
 }
avatar image
1

Answer by Owen-Reynolds · Aug 21, 2015 at 04:52 PM

It doesn't matter whether you do or don't use new on Vectors.

Vector3s are structs. This official microsoft C# page explains that using new for structs isn't a real new. It's just a way to force it to be initialized:

https://msdn.microsoft.com/en-us/library/0taef578.aspx

The same page also explains how structs aren't really objects, the way classes are. So Vector3 doesn't make an object, since it can't.

If reading that page hurts your head, that's the main idea. The language and the compiler are often doing a lot that you can't see. You generally have to trust that the usual way to do things (like, official Unity examples using new with Vector3s) gets handled well-enough.

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 Aka_ToolBuddy · Jun 09, 2020 at 07:58 PM 0
Share

I agree, the "new" call should not do anything on a struct memory wise, but the $$anonymous$$ono compiler used in Unity does not make the optimization of ignoring the constructor call. You can find more information, with IL instructions comparison, at this link: https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338/

avatar image
0

Answer by Aka_ToolBuddy · Jun 09, 2020 at 08:01 PM

Yes, you should avoid calling new if you want to get performance. Of course I am speaking about situations where you have a LOT of vector operations for this to make a difference. A scenario where this can happen is mesh generation/transformation.

If you don't call the constructor, then you have to assign all the vector's component before being able to use it, otherwise the compiler will not be happy.

You don't have to bother doing this if you use this asset I made that does that automatically for you

The asset: Frame Rate Booster

The forum explaining how things work: https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338/

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

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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

2d camera help! 0 Answers

On Trigger Stay 1 Answer

Problems instantiating a prefab (javascript) 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