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
1
Question by Fattie · Apr 04, 2013 at 10:21 AM · javascriptgetcomponent

Unityscript - Which GetComponent is faster ?

Here are two versions of GetComponent in Unityscript

Here's one..

 private var brain:BrainScript;
 brain = head.GetComponent(BrainScript);

And here's the other, the Generic ..

 private var brain:BrainScript;
 brain = head.GetComponent.<BrainScript>();

Does anyone know: is one faster than the other?

(Note .. you can also I believe use GetComponent by passing it a string. So, GetComponent("BrainScript") would also work. If you do that, it is very slow. Generally one would never do that. I'm not asking about that. I'm asking about the two variations above where you pass in the type.)

Does anyone know if there's any speed difference, or indeed any difference at all, between the two?

Cheers!

Comment
Add comment · 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 yusolaifdfer · Apr 04, 2013 at 10:37 AM 1
Share

In C# generics do have a performance benefit.

avatar image Fattie · Apr 04, 2013 at 10:39 AM 0
Share

that's good to know. I'm wondering about Unityscript here

avatar image whydoidoit · Apr 04, 2013 at 10:39 AM 1
Share

I don't know - but I think that they are exactly the same. GetComponent(BLAH) is effectively using the generic I believe but it avoids the beginner unfriendly syntax.

avatar image Eric5h5 · Apr 04, 2013 at 03:44 PM 0
Share

@yusolaifdfer: the generic version of GetComponent has a performance penalty, both in C# and Unityscript. Furthermore, a generic List is slower than a built-in array of the same type, by quite a lot in the case of primitive types. Generics are only faster when compared to something like ArrayList, which requires boxing/unboxing.

@whydoidoit: GetComponent(Blah) is effectively doing GetComponent(Blah) as Blah, not the generic version, which as demonstrated by Gerry$$anonymous$$ (and mentioned by myself on many occasions ;) ) is a little slower.

1 Reply

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

Answer by GerryM · Apr 04, 2013 at 11:05 AM

Interesting, after testing it with this script, it seems the first version is slightly faster (about 1.4) than the second version (about 1.7). You might want to check for yourself in your specific setting, though.

 function Start () {
 
   var sw = new System.Diagnostics.Stopwatch();
   sw.Start();
 
   for (var i = 0; i < 10000000; i++)
   {
     var brain = GetComponent(BrainScript); // Version 1
     //var brain = GetComponent.<BrainScript>(); // Version 2
   }
 
   sw.Stop();
   Debug.Log("Elapsed = "+sw.Elapsed);
 }
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 Fattie · Apr 04, 2013 at 11:10 AM 1
Share

holy testing, batman ! that's amazing..

BTW System.Diagnostics.Stopwatch(); incredible tip, thanks

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

15 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

Related Questions

How can i call GUI.tooltip from another script 1 Answer

How to add points to score using GetComponent 2 Answers

GetComponent of type UnityScript from Boo Script 1 Answer

Shared variable problem, between scripts, help? 1 Answer

Unity3d Update issue 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