• 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
Question by augary · Feb 28, 2013 at 05:29 PM · prefabrenderingperformance

Not a question - just some performance metrics for rendering from Unity scripts

I need to dynamically instantiate and alter GameObjects from scripts and wanted to share what I found about the performance of these scripts. I've included the C# source code at the bottom.

Testing protocol: I am running Unity 4.0.1f2. The script below was attached to the Main Camera object, and references a simple prefab Cube called myCube. For the tests below, I took the best of 3 times to try and minimize reflection of any temporary bottlenecks or CPU overload created by other programs running on my mac$$anonymous$$ne (Lenovo G560 running 64-bit Windows 7, 2.13GHz, 4.0GB RAM). Test result is the average seconds per render over 1000 renders.

1000 cubes, static: 0.01022 seconds per render

1000 cubes, floating away along the z-axis (1), using new Vector3: 0.01316 s/render

1000 cubes, floating away along the z-axis (1), using static Vector3.forward: 0.01362 s/render

At t$$anonymous$$s point, I was surprised that multiplying a static vector by the time elapsed generated worse results than instantiating new vectors for each cube on each render, but the order of magnitude of the difference here was lower to a point that I just decided to continue using new Vector3 instantiation for each test below and assume results would be comparable with the static Vector3.

1000 cubes, stretc$$anonymous$$ng along one axis (2): 0.01691 s/render

Benchmarking the cost of generating Random values: 1000 cubes, on each render generating a Random.range value for each cube (but doing not$$anonymous$$ng with it): .01042 s/render

After t$$anonymous$$s, I did not include any further metrics to assess cost of the Random float generator, since the order or magnitude of the difference is again much smaller, and probably comparable in order of magnitude to the variance caused by my admittedly rudimentary testing method.

1000 cubes, enabling/disabling each on each render according to a random value (3): .01928 s/render

1000 cubes, floating away along the z-axis (1) w$$anonymous$$le also stretc$$anonymous$$ng along one axis (2): 0.01809 s/render

1000 cubes, floating away along the z-axis (1) w$$anonymous$$le also enabling/disabling randomly (3): 0.02261 s/render

1000 cubes, stretc$$anonymous$$ng along one axis (2) w$$anonymous$$le also enabling/disabling randomly (3): 0.03292 s/render

1000 cubes, floating away along the z-axis (1), stretc$$anonymous$$ng along one axis (2), and enabling/disabling randomly (3): 0.03404 s/render

Hope t$$anonymous$$s helps!

Source code:

 using UnityEngine;
 using System.Collections;
 
 public class makeCube : MonoBehaviour {
     
     public static int num_shapes=1000;
     
     public GameObject myCube;
     
     float lastRenderTime;
     Vector3[] v3array;
     GameObject[] oarray;
     
     int renders;
     float totalRenderingTime;
     
     void Start () {
         renders=0;
         totalRenderingTime=0.0F;
         
         lastRenderTime = Time.time;
         
         v3array = new Vector3[num_shapes];
         oarray = new GameObject[v3array.Length];
         for (int i=0; i<v3array.Length; i++)
         {
             v3array[i] = new Vector3(((float)i)/400.0F,((float)i)/250.0F,((float)i)/500.0F);
         }
         Quaternion q = Quaternion.identity;
 
         for(int i=0; i<v3array.Length; i++)
          {
             oarray[i] = (GameObject)Instantiate(myCube, v3array[i], q);
         }
     }
     
     void Update () {
     
         renders++;
         totalRenderingTime+=(Time.time-lastRenderTime);
         if (renders%100==0)
             print (renders + " renders in " + totalRenderingTime + " seconds");
         
         float enableRandom=0;
         
         for(int i=0; i<v3array.Length; i++)
         {
             // TRANSFORM 1: Floating away along the z-axis
             //oarray[i].transform.position += Vector3.forward * (Time.time-lastRenderTime);
             //oarray[i].transform.position += new Vector3(0.0F,0.0F,(Time.time-lastRenderTime));
             
             //TRANSFORM 2: Stretc$$anonymous$$ng along one axis
             //oarray[i].transform.localScale += new Vector3((Time.time-lastRenderTime),0.0F,0.0F);
             
             //TRANSFORM 3: Enabling/disabling randomly
             /*enableRandom = Random.Range(-1.0F, 1.0F);
             if (enableRandom > 0)
             {
                 oarray[i].SetActive(true);
             }
             else
             {
                 oarray[i].SetActive(false);
             }*/
         }
         lastRenderTime = Time.time;
     }
 }
Comment
neonblitzer

People who like this

-1 Show 4
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 Itinerant · Feb 28, 2013 at 08:20 PM 0
Share

This'd be good to post in the forums: questions here get pushed through the list pretty quickly, and I can see this being worth some conversation.

avatar image Bunny83 · Feb 28, 2013 at 09:00 PM 1
Share

Questions that start with "Not a question" are quite obviously wrong here. This is a Q&A site. We have a forum and a wiki beside the Q&A site, so why did you post it as question? I don't get it...

Also this kind of benchmark usually doesn't "prove" anything. It should be done several times under same conditions and repeated on different hardware. Also it's not clear if all objects are visible all the time or if some got frustum culled. Also is is any kind of batching happening?

Next thing is print (or Debug.Log which is called by print) has a horrible performance in the editor. So calling it during a benchmark totally destroys the result, even when only called every 100 frames.

Vector3.forward is not a constant value. It's just a static property that is implemented like this:

     // extracted from UnityEngine.dll
     public static Vector3 forward
     {
         get { return new Vector3(0f, 0f, 1f); }
     }

Time.time is not realtime. It is the time the current frame has started you calculate Time.deltaTime manually but it will result in the same value. For benchmark time measurement you should use a reliable realtime counter. If you don't need a very high precision you can use Time.realtimeSinceStartup, otherwise the .NET / Mono framework has some classes that might help (Stopwatch, on windows there's also QueryPerformanceCounter)

avatar image augary · Mar 01, 2013 at 03:25 PM 0
Share

re Itinerant: Thanks - was not aware Unity had forums, I will move this post there. It will be listed under the same title except with "Not a question" removed.

avatar image augary · Mar 01, 2013 at 03:34 PM -1
Share

re Bunny: as mentioned above, I will be moving this to forums. Please feel free to comment there and I'll be happy to respond, but I wanted to make two observations before you do: first, the purpose here was to measure relative performance, so print() won't be messing up the benchmark as it happens on every iteration. It's like if we tried to measure whether you run slower with a 50-pound sack on your back: we wouldn't have to replace your legs with rocket skates before the experiment. second, when my company makes the results of our internal research available for free to the developer community, you can feel free to not use it if you don't find it useful. I've also made the source code available, for the exact reason that you may run a different test more suited to your own objectives if you'd like.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Modifying PlayerSettings for RenderPath 1 Answer

Best Practice For Shader Complexity + IOS 1 Answer

Are the prefabs stored in RAM or in the Hard disk? 0 Answers

Trying to assign GUIText from heirarchy into instantiated prefab 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