- Home /
Performance implications of namespaces in Unity.
I would like to get some community feedback here. I see a lot of discussion out there about the performance differences between the logical and syntax aspects between C# and UnityScript. However, some of my colleagues and I are debating about the performance implications of using namespaces in the unity runtime. It has been asserted by someone that simply using namespaces causes degradation over the entire runtime and lowers frame rate. If I have missed an existing post somewhere covering this specifically could you please reference it?
Does the use of namespaces cause performance degradation? If so, when is this degradation realized in the lifecycle of the scene graph (Awake, continuous, etc)? Is it only while loading any imported assemblies or any time a MonoBehavior using a custom namespace is created (for example)?
Edit(6/29): Because of the responses here and some other discussions I have read on the web (of which there is a very good one here), I think this myth is BUSTED! The act of using namespaces does not impact runtime performance of code with the exception of using the reflection API. It's largely a compile time effort. The generated IL (using UnityScript or C#) has fully qualified classes anyway. Also, if this were true then the use of all UnityEngine and UnityEditor classes would be "degrading performance".
Answer by Mike 3 · Jul 28, 2010 at 06:38 PM
If you're talking about using namespaces around your own code, then no - it's something which doesn't make any difference outside of compiling and using reflection
If you're talking about using namespaces at the top of files, it can add to the build size, and any of the code used from those namespaces could cause slowdowns - but that has nothing to do with the namespace itself
Answer by Eric5h5 · Jul 28, 2010 at 07:21 PM
If you mean something like
import System;
function Start () { print (DateTime.Now); }
as opposed to
function Start () {
print (System.DateTime.Now);
}
then those are the exact same thing. Importing a namespace doesn't "do" anything and can't affect performance in any possible way. If you have import System.Security.Cryptography in your script but don't actually use any functions from System.Security.Cryptography, then nothing will happen.
It just makes typing stuff more convenient. With C#, for example, having using UnityEngine in your script just means you can type Vector3 instead of UnityEngine.Vector3. (The reason why you don't have to do import UnityEngine in JS is because it has the UnityEngine namespace imported automatically, since it's written specifically for Unity and naturally you're going to be using the UnityEngine namespace almost all the time.)
I am asking does the fact that classes use namespaces other than the default negatively impact performance. I get fully qualifying classes in the code vs using and understand the JS scenario. Someone has made the assertion to me that "namespaces cause a degradation in performance" and I don't agree with that as a general statement. I understand there are caveats probably for reflection and some other instances, etc but the act of referencing a class from another namespace should not (or negligibly) affect performance. With the "using statement" or otherwise. But I wanted to get opinions.
Your answer