Efficiency tests for direct use of transform and gameobject in unity

Source: Internet
Author: User

The letter is better than no book, not to mention the online developer's blog. Here also contains my own blog, sometimes back to see the wrong place, but I also too lazy to change, and many times we see the article is not necessarily the original address, but by the various sites seven turn eight, more impossible to guarantee accuracy.

This test says: "Do not use transform or gameobject directly in the script, but should be cached as member variables such as _transform and _gameobject to be accessed at the beginning." Because the former each time to obtain the corresponding components, low efficiency. ”

This statement cannot be said to be completely wrong, but after your own tests understand the principle, you will have a clearer understanding of the problem and its similar problems.

First of all, we try to use the Getcomponent cache to the member variables at the beginning of the component, which is perfectly reasonable. But Tansform and gameobject are such common objects that unity should have better optimizations. Otherwise it would seem too low. The result of my test is true. If you don't want to look at the following analysis, just look at the conclusion.

Conclusion: Direct access to transform efficiency in unity is a little slower than caching a member variable, which is only reflected when the magnitude is very large. Unity has enough optimizations for transform and gameobject, not to emulate the unity write properties themselves, and most of the cases are less efficient.


Please see:


The test code is a simple transform coordinate assignment that loops 100w times.

The Unity attribute is the time consumption of direct access to the transform, while the cached variable is the version of the member variable. You can see that the direct cache member variable is actually faster, but 100w execution, the performance difference is about 10%. Personal feeling this difference has little effect. Whether to use Unity's properties directly or to cache member variables to see personal habits.

Note that the following two properties are judged and attributes are directly obtained from the variable. We may be "self-righteous" to write this, if the _transform is empty, then get the corresponding component, otherwise directly return the member variable. This is the slowest "attribute to judge", at which point the property performs relatively complex logic, and the slowness is reasonable. If a property returns a member variable directly, its efficiency is consistent with the direct access member variable.


The test code is as follows:

Using unityengine;using System.collections;public class Testtransform:monobehaviour {private Gameobject _go;    Private Transform _tr;        Protected Transform Simpletr {get {return _tr;            }} protected Transform Tr {get {if (_tr = = null) {_tr = Transform;        } return _TR;        }} protected Gameobject Simplego {get {return _go;            }} protected Gameobject Go {get {if (_go = = null) {_go = Gameobject;        } return _go;        }}void Start () {_go = Gameobject;        _TR = transform;        Dotesttransform ();    Dotestgameobject ();        } public void Dotesttransform () {int count = 1000000;        Transform.position = Vector3.zero;        int time = System.Environment.TickCount; for (int i = 0; i < count; ++i) {int index = i % 100;        transform.position = Vector3.one * INDEX;        } Debug.Log ("Unity attribute:" + (system.environment.tickcount-time) * 1000);        _tr.position = Vector3.zero;        int time2 = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100;        _tr.position = Vector3.one * INDEX;        } Debug.Log ("Cached variable:" + (system.environment.tickcount-time2) * 1000);        Tr.position = Vector3.zero;        int time3 = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100;        tr.position = Vector3.one * INDEX;        } Debug.Log ("attribute to judge:" + (SYSTEM.ENVIRONMENT.TICKCOUNT-TIME3) * 1000);        Simpletr.position = Vector3.zero;        int time4 = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100;        simpletr.position = Vector3.one * INDEX; } Debug.Log ("property to get the variable directly: "+ (SYSTEM.ENVIRONMENT.TICKCOUNT-TIME4) * 1000);        } public void Dotestgameobject () {int count = 1000000;        GameObject.transform.position = Vector3.zero;        int time = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100;        gameObject.transform.position = Vector3.one * INDEX;        } Debug.Log ("Unity attribute:" + (system.environment.tickcount-time) * 1000);        _go.transform.position = Vector3.zero;        int time2 = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100;        _go.transform.position = Vector3.one * INDEX;        } Debug.Log ("Cached variable:" + (system.environment.tickcount-time2) * 1000);        Go.transform.position = Vector3.zero;        int time3 = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100; Go.transform.position= Vector3.one * INDEX;        } Debug.Log ("attribute to judge:" + (SYSTEM.ENVIRONMENT.TICKCOUNT-TIME3) * 1000);        SimpleGo.transform.position = Vector3.zero;        int time4 = System.Environment.TickCount;            for (int i = 0; i < count; ++i) {int index = i% 100;        SimpleGo.transform.position = Vector3.one * INDEX;    } Debug.Log ("attribute directly gets variable:" + (SYSTEM.ENVIRONMENT.TICKCOUNT-TIME4) * 1000); }}


Efficiency tests for direct use of transform and gameobject in unity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.