Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Double-V · Nov 25, 2016 at 06:29 PM · scripting problemperformanceoptimizationnamespace

How using additional namespaces affects performance?

I wonder how adding something along the lines of:

using System.Collections.Generic

or

using UnityEngine.UI

affects performance or size of the project. Is it significant? I ask, because I'd like to change packs of regular variables into dictionaries, but I don't know if it is worth it.

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

1 Reply

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

Answer by tanoshimi · Nov 25, 2016 at 06:35 PM

It has zero effect on either the performance or size of a project.

Comment
Add comment · Show 5 · 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 Double-V · Nov 25, 2016 at 06:46 PM 0
Share

Hmm, but why is that? It seems counter-intuitive.

avatar image tanoshimi Double-V · Nov 25, 2016 at 08:22 PM 2
Share

Not at all. All the using statement does is allow you to reference methods and types from that namespace in that code without fully qualifying them. So, ins$$anonymous$$d of having to write:

 System.Collections.Generics.List<int> myList = new System.Collections.Generics.List<int>();

You can write:

 using System.Collections.Generic;
 List<int> myList = new List<>();

The contents of the Systems.Collections.Generic namespace is included in your project either way, and the List type itself works in exactly the same way either way (because, when compiled to CIL, all code references become fully-qualified anyway)

avatar image aphenine Double-V · Nov 25, 2016 at 11:51 PM 0
Share

It's not counter intuitive if you understand that the code gets compiled first. The compiler has to do all the heavy lifting when it comes to working out the name spaces and what's being used and it only has to do that once, when the code is being assembled.

A few namespaces might cause extra things to get included in the compilation phase (I saw somewhere that System.Xml adds stuff), but that's about it.

avatar image MCudlitz Double-V · Nov 26, 2016 at 12:12 AM 1
Share

When you use a namespace, you aren't actually loading extra stuff, only exposing it. If you import way more than you should, the downside is that you pollute your namespace, resulting in usability problems (I.$$anonymous$$ when you import System and you end up not being able to call Unity's Random function with simply Random.Next() anymore because it's not sure if you are referring to the system or the unity function).

avatar image Bunny83 MCudlitz · Nov 26, 2016 at 12:39 AM 1
Share

Right, that's a general misconception i have encountered a few times now. The namepace where a class is defined in and where it's physically stored are two completely unrelated things. It's true that classes are often placed in a namespace that is named after the assembly where they are defined in, but not in all cases. A good example are things like System.Collections.Generic.List<T> or System.Func<T> they are all defined in the mscorlib.dll (the mono / .NET core assembly).

On the other hand System.Collections.Generic.Stack<T> is defined inside the "System.dll". Note the generic List and Stack are in the same namespace but in completely different assemblies.

Putting using System.Xml; at the top of a script doesn't include any additional assembly when you build your game, only when you actually use something from that namespace as all those classes are defined in the System.Xml.dll assembly.

The using statement at the top of a script is just a way to "shorten" the full class name. And that's why the compiler gets confused when you use two different namespaces which contain the same class. The most common example in Unity is System.Random and UnityEngine.Random.

When you have using UnityEngine; as well as using System; at the top and you try to use the Random class you will get a compiler error that he doesn't know which one he should use. In such cases you can either use the full qualified class name UnityEngine.Random.Range(1,5) or put an extra using statement like this at the top:

 using Random = UnityEngine.Random;

This makes it clear that whenever you refer to "Random" inside this script you want to use Unity's Random class.

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

93 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

Related Questions

How Can I Reduce Build Time for Script-Heavy Projects? 2 Answers

How to optimize with a lot of objects 0 Answers

A very simple optimization question, particle 1 Answer

Double Buffer and/or Mesh.MarkDynamic in Unity 5 0 Answers

Executing a script before loading assets of scene into memory 0 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