Hi, developers! What should work faster? What's better to use? Why? 1) Code (csharp): CollisionBase t = Collided[Collided.Count - 1]; if (t is Human) Debug.Log("H"); 2) Code (csharp): Type t = Collided[Collided.Count - 1].GetType(); if (t == typeof(Human)) Debug.Log("H"); Thanks!
If you have a really technical question about programming... this is the place to go: http://stackoverflow.com/questions/184681/is-vs-typeof
It's not the same functionality - that's far more important than performance optimisation. If either can work in your use-case it's time to benchmark.
http://forum.unity3d.com/threads/160453-C-switch-is-type-typeof()-GetType() Seems like it doesn't works in Unity or something like that...
Or... You can't use a Type in a switch statement as the error says. Don't compare apples and oranges.
Thanks for reply. But I don't get what should I do. I should use if statement or I could use switch statement, but in the different way?..
comparing the types though only lets you know if the type of your object is directly the other type. it checks inheritance wise as well. class A class B : A B obj = new B(); if(obj is A) //is true if(obj.GetType() == typeof(A)) //is false
Yes I read that in the article, in my case that optimization works just right and I tested it successfully. Thanks.
1 consider that the link that the stackoverflow thread refers to is using .net and not mono, you should test this first before assuming that it's going to be faster. The obj.GetType() == typeof(SomeType) is optimized by the jit, but mono is a different jit than the .net jit... so it might not get optimized the same way. 2... really? How often are you testing the type of something that this small optimization is giving you some edge? You think this is "critical code"? Cause I'd beg to say that if this is a bottle neck in your code... your critical code would be the fact you can't write good code. Type checking like this shouldn't be common.
You are right. I just made my own benchmark for different types and the conclusion is very interesting. Finally decided to use one of the methods to compare types, thanks!
Don't get bogged down in this type of "optimisation". It takes time away from you finishing your project and you don't even know that it's a problem. Code that *looks* sub-optimal to a programmer's eye is often just fine as far as your computer is actually concerned because there's often bigger bottlenecks elsewhere in code or design, or because you don't actually have a performance issue in the first place and therefore all optimisation is moot. Measure before you optimise, and only bother to optimise where the measurements show you're actually wasting resources.
It's ok, I didn't take it as a important optimization, it's just something easy to replace in the code, and easy to turn into a habit.
Not at all And my hobby of collecting silly optimizations doesn't affect my other hobby of developing advanced game mechanics: http://forum.unity3d.com/threads/153492-Best-way-to-start-with-Inverse-Kinematic-Ragdoll http://forum.unity3d.com/threads/158678-Ideas-for-discovering-internals-of-a-lock