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 CardboardComputers · Jun 30, 2018 at 08:44 PM · vector3performance optimizationimplementation

Performance of Vector Addition vs. Component Addition

Hey Unity! The Vector3 is a mutable data type with respect to its components. If you have, say,

 Vector3 a = Vector3.up;
 Vector3 b = Vector3.right;

You can get the additive resultant as either

 return a + b;

or

 return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);

but which one is faster? I'm inclined to think that the internal implementation of the first option is just the second option, and in any case I doubt if the first option can be slower than the second, but the docs for the addition operation don't specify, and as far as I know it is technically possible that it's instead implemented

 Vector3 operator+(const Vector3& other) {
     this->x += other.x;
     this->y += other.y;
     this->z += other.z;
     return this;
 }

which should be faster than the second option since you're not going out of your way to allocate memory for a brand new Vector3 object.

Comment
Add comment · Show 2
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 Bunny83 · Jun 30, 2018 at 09:21 PM 0
Share

Note that the source code of the UnityEngine.dll is available for reference over here. Note that it's only up for reference. $$anonymous$$ake sure you read the licence and copyright information given in the readme.


Second you seem to confuse C# and C++ code as well as objects vs structs. Vector3 is a struct and therefore a value type. The constructor of the Vector3 struct does not allocate any memory.

avatar image CardboardComputers Bunny83 · Jul 01, 2018 at 04:23 AM 0
Share

...Y-yeah! That sounds about right actually. Whups. Can't ask questions when tired I guess, I couldn't even wrap my head around an array at the time > >

2 Replies

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

Answer by Hellium · Jun 30, 2018 at 08:55 PM

Here is the implementation of the + operator overload in the Unity engine.

     // Adds two vectors.
     public static Vector3 operator+(Vector3 a, Vector3 b) { return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z); }


The problem with the code you suggest is that one of the two vectors involved in the operation will be irremediably changed. However, when you do 1 + 2, 1 and 2 don't change, it wouldn't be logical if it happened with the + operator of the Vector struct.


But, keep in mind that your concerns should not exist. This is micro-optimisation your user will never notice, unless you compute hundreds of vectors each frame.

Comment
Add comment · Show 3 · 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 Bunny83 · Jun 30, 2018 at 09:13 PM 0
Share

You could probably replace hundreds with thousands or millions ^^. Though i've actually done a test on manually inlining code and got a massive speed improvement.(8x faster). Though this was only tested inside the editor. In an actual build the small operators / methods may be inlined automatically. Though manually expanding / inlining the code in my case allowed for some additional simplifications which is probably the main reason for the speed up.

avatar image Aka_ToolBuddy Bunny83 · Jun 09, 2020 at 07:45 PM 0
Share

The main reason of the speed up is the lack of call to the vector3's constructor. You can find more detail about that subject in the link I posted bellow. And the optimization works also in a build

avatar image CardboardComputers · Jul 01, 2018 at 04:20 AM 0
Share

Ooooh you're right! That was a silly of me. pff

avatar image
0

Answer by Aka_ToolBuddy · Jun 09, 2020 at 07:42 PM

You might find useful an asset that I made that implements optimizations like the one you posted about. You can find more about the subject here: https://forum.unity.com/threads/vector3-and-other-structs-optimization-of-operators.477338 The actual asset is : Frame Rate Booster

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

112 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 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 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 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

EnemyHover Script Error? 0 Answers

Calculate vector exactly between 2 vectors 4 Answers

Tic Tac Toe--how to check for win 2 Answers

How to smooth my camera (Vector3.Lerp) 0 Answers

Vector3 Projection 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