- Home /
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.
Answer by tanoshimi · Nov 25, 2016 at 06:35 PM
It has zero effect on either the performance or size of a project.
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)
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.
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).
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
![](https://koobas.hobune.stream/wayback/20220612101414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Voxel learning project performance issues 0 Answers
Sprites performance issue 0 Answers
How Can I Reduce Build Time for Script-Heavy Projects? 2 Answers
How to optimize with a lot of objects 0 Answers