Search Unity

C#: typeof or is?

Discussion in 'Scripting' started by Harter, Nov 28, 2012.

  1. Harter

    Harter

    Joined:
    Feb 28, 2012
    Posts:
    119
    Hi, developers!

    What should work faster? What's better to use? Why?

    1)
    Code (csharp):
    1.         CollisionBase t = Collided[Collided.Count - 1];
    2.         if (t is Human)
    3.             Debug.Log("H");
    4.        
    2)
    Code (csharp):
    1.         Type t = Collided[Collided.Count - 1].GetType();
    2.         if (t == typeof(Human))
    3.             Debug.Log("H");
    4.        
    Thanks!
     
    Last edited: Nov 28, 2012
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
  3. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    225
    Hmm, so anObj.GetType() == typeof(string) is faster. Time to optimize code!
     
  4. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    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.
     
  5. Harter

    Harter

    Joined:
    Feb 28, 2012
    Posts:
    119
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Or... You can't use a Type in a switch statement as the error says. Don't compare apples and oranges.
     
  7. Harter

    Harter

    Joined:
    Feb 28, 2012
    Posts:
    119
    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?..
     
  8. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    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
     
    timmccune likes this.
  10. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    225
    Yes I read that in the article, in my case that optimization works just right and I tested it successfully. Thanks.
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    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.
     
  12. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    225
    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!
     
  13. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    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.
     
  14. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    225
    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.
     
  15. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    just don't let that habit cover over the fact that the functionality is different
     
  16. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    225