Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. optimization of type comparison

optimization of type comparison

Scheduled Pinned Locked Moved C#
csharpalgorithmsperformancequestioncode-review
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spin vector
    wrote on last edited by
    #1

    I'm overriding an Equals() method in .NET which a type specialization in mind but don't want to kill my code in stupid ways. So: public override bool Equals(object obj) { if ( obj.GetType() == typeof(double) ) // foo else return base.Equals(obj) } The line to optimize is "if ( obj.GetType() == typeof(double) )". The constant typeof(double) calls is a waste. Tried "if ( obj.GetType().Name == "System.Double" )" with horrible results. Any optimizations out there? Thx!

    C L 2 Replies Last reply
    0
    • S spin vector

      I'm overriding an Equals() method in .NET which a type specialization in mind but don't want to kill my code in stupid ways. So: public override bool Equals(object obj) { if ( obj.GetType() == typeof(double) ) // foo else return base.Equals(obj) } The line to optimize is "if ( obj.GetType() == typeof(double) )". The constant typeof(double) calls is a waste. Tried "if ( obj.GetType().Name == "System.Double" )" with horrible results. Any optimizations out there? Thx!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You could store the result of this call to a variable that's used by the comparison.

      Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

      1 Reply Last reply
      0
      • S spin vector

        I'm overriding an Equals() method in .NET which a type specialization in mind but don't want to kill my code in stupid ways. So: public override bool Equals(object obj) { if ( obj.GetType() == typeof(double) ) // foo else return base.Equals(obj) } The line to optimize is "if ( obj.GetType() == typeof(double) )". The constant typeof(double) calls is a waste. Tried "if ( obj.GetType().Name == "System.Double" )" with horrible results. Any optimizations out there? Thx!

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        if (obj is double) ... else ...

        Luc Pattyn

        S 1 Reply Last reply
        0
        • L Luc Pattyn

          if (obj is double) ... else ...

          Luc Pattyn

          S Offline
          S Offline
          spin vector
          wrote on last edited by
          #4

          if (obj is double) wins! Gives me 2x speed over obj.GetType() == typeof(double). Thanks!

          L 1 Reply Last reply
          0
          • S spin vector

            if (obj is double) wins! Gives me 2x speed over obj.GetType() == typeof(double). Thanks!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            As far as performance goes, there are only two rules: 1) shorter code is always faster 2) rule 1 is false :)

            Luc Pattyn

            C 1 Reply Last reply
            0
            • L Luc Pattyn

              As far as performance goes, there are only two rules: 1) shorter code is always faster 2) rule 1 is false :)

              Luc Pattyn

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I didn't think that 'is' would work for a value type ? Or is that just 'as' that won't work ( as it can't return null ) ???

              Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

              L 1 Reply Last reply
              0
              • C Christian Graus

                I didn't think that 'is' would work for a value type ? Or is that just 'as' that won't work ( as it can't return null ) ???

                Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Sure is is always OK (it produces a bool) whereas as is not. :)

                Luc Pattyn

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups