Static methods slower?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?B?QW5kcuk=?=

    Static methods slower?

    Hello,

    I'm having a discussion with my colleagues (and boss) over the use of
    static methods in a class.

    My colleagues say that static methods should be avoided whenever is
    possible, because their use slows down the application.

    For me, this sounds absurd - I believe that static methods should be
    used whenever they're necessary. However, I wasn't able to find any
    reliable documents on that. Does anyone knows of a reliable source
    (like Microsoft) that discusses that?

    Thank you in advance,

    André

  • Andy

    #2
    Re: Static methods slower?

    That's just a silly argument. Not all methods make sense at a instance
    level.

    Instead of trying to prove them wrong, ask them to prove that static
    methods are in fact slower. If anything, I would think they are
    faster, because they are not overridable and don't have to look in a
    virtual table, IIRC.

    Also, ask they why they are concerned about performance of a method
    call, when .Net has garbage collection, boxing / unboxing, etc.

    If you're programming in .Net, you should have already decided you're
    not going to worry about nanoseconds of performance. If you are, you
    should be writing your code in C or assembly..

    Andy

    André wrote:
    Hello,
    >
    I'm having a discussion with my colleagues (and boss) over the use of
    static methods in a class.
    >
    My colleagues say that static methods should be avoided whenever is
    possible, because their use slows down the application.
    >
    For me, this sounds absurd - I believe that static methods should be
    used whenever they're necessary. However, I wasn't able to find any
    reliable documents on that. Does anyone knows of a reliable source
    (like Microsoft) that discusses that?

    Thank you in advance,

    André

    Comment

    • =?iso-8859-1?B?QW5kcuk=?=

      #3
      Re: Static methods slower?

      That's just a silly argument. Not all methods make sense at a instance
      level.
      >
      Instead of trying to prove them wrong, ask them to prove that static
      methods are in fact slower. If anything, I would think they are
      faster, because they are not overridable and don't have to look in a
      virtual table, IIRC.
      >
      Also, ask they why they are concerned about performance of a method
      call, when .Net has garbage collection, boxing / unboxing, etc.
      >
      If you're programming in .Net, you should have already decided you're
      not going to worry about nanoseconds of performance. If you are, you
      should be writing your code in C or assembly..
      Hello, Andy,

      I positively agree with you. But, unfortunately, unless I provide some
      reliable documentation (like Microsoft's) on that, I can't have my
      point made... :-(

      André

      Comment

      • Andy

        #4
        Re: Static methods slower?

        André wrote:
        I positively agree with you. But, unfortunately, unless I provide some
        reliable documentation (like Microsoft's) on that, I can't have my
        point made... :-(
        I suggestion you move on then, no sense in working with people that
        have such absurd beliefs.

        Here's one link I found that actually claims statics are faster, from
        an MVP..



        Comment

        • Andy

          #5
          Re: Static methods slower?

          Oh, and again, I would really push them against the wall and demand
          them to back up their point with some documentation.

          Comment

          • Dustin Campbell

            #6
            Re: Static methods slower?

            I'm having a discussion with my colleagues (and boss) over the use of
            static methods in a class.
            >
            My colleagues say that static methods should be avoided whenever is
            possible, because their use slows down the application.
            >
            For me, this sounds absurd - I believe that static methods should be
            used whenever they're necessary. However, I wasn't able to find any
            reliable documents on that. Does anyone knows of a reliable source
            (like Microsoft) that discusses that?
            You're right. That's absurd. Instance methods have more overhead than static
            methods.

            Here's a very simple console application (build with .NET Framework 2.0)
            that will demonstrate the difference:

            using System;
            using System.Diagnost ics;

            namespace StaticMethodSpe edTest
            {
            class Program
            {
            static double Average(long[] values)
            {
            double sum = 0;
            for (int i = 0; i < values.Length; i++)
            sum += values[i];

            return sum / values.Length;
            }

            class TestClass
            {
            public void InstanceMethod( )
            {
            }
            public static void StaticMethod()
            {
            }
            }

            static void Main(string[] args)
            {
            const int NUM_SAMPLES = 1000;
            const int NUM_CALLS = 1000000;

            long[] samples = new long[NUM_SAMPLES];
            Stopwatch watch = new Stopwatch();

            for (int i = 0; i < NUM_SAMPLES; i++)
            {
            watch.Reset();
            watch.Start();

            for (int j = 0; j < NUM_CALLS; j++)
            TestClass.Stati cMethod();

            watch.Stop();

            samples[i] = watch.ElapsedMi lliseconds;
            }

            Console.WriteLi ne("{0:###,###, ###,##0} static method calls took approximately
            {1:0.######} seconds", NUM_CALLS, Average(samples ) / 1000);

            TestClass c = new TestClass();
            for (int i = 0; i < NUM_SAMPLES; i++)
            {
            watch.Reset();
            watch.Start();

            for (int j = 0; j < NUM_CALLS; j++)
            c.InstanceMetho d();

            watch.Stop();

            samples[i] = watch.ElapsedMi lliseconds;
            }

            Console.WriteLi ne("{0:###,###, ###,##0} instance method calls took approximately
            {1:0.######} seconds", NUM_CALLS, Average(samples ) / 1000);
            }
            }
            }

            I get the following results:

            1,000,000 static method calls: 0.006008
            1,000,000 instance method calls: 0.006152

            As you can see, the difference is extremely small but the edge goes to static
            methods.




            Best Regards,
            Dustin Campbell
            Developer Express Inc.


            Comment

            • Brian Gideon

              #7
              Re: Static methods slower?

              André,

              If I had to make a guess I'd say static methods are generally faster
              for the same reasons Andy stated.

              Regardless, is the marginal performance difference really worth
              sacrificing correctness and readability?

              Brian

              André wrote:
              Hello,
              >
              I'm having a discussion with my colleagues (and boss) over the use of
              static methods in a class.
              >
              My colleagues say that static methods should be avoided whenever is
              possible, because their use slows down the application.
              >
              For me, this sounds absurd - I believe that static methods should be
              used whenever they're necessary. However, I wasn't able to find any
              reliable documents on that. Does anyone knows of a reliable source
              (like Microsoft) that discusses that?

              Thank you in advance,

              André

              Comment

              • Dustin Campbell

                #8
                Re: Static methods slower?

                >I positively agree with you. But, unfortunately, unless I provide
                >some reliable documentation (like Microsoft's) on that, I can't have
                >my point made... :-(
                >>
                I suggestion you move on then, no sense in working with people that
                have such absurd beliefs.
                >
                Here's one link I found that actually claims statics are faster, from
                an MVP..
                >
                http://forums.microsoft.com/MSDN/Sho...47372&SiteID=1
                That MVP is correct. The majority of savings is because the 'this' pointer
                does not need to be loaded onto the stack before calling a static method.
                I would love to know where these co-workers came up with such a ridiculous
                notion.

                Best Regards,
                Dustin Campbell
                Developer Express Inc.


                Comment

                • =?iso-8859-1?B?QW5kcuk=?=

                  #9
                  Re: Static methods slower?

                  That MVP is correct. The majority of savings is because the 'this' pointer
                  does not need to be loaded onto the stack before calling a static method.
                  I would love to know where these co-workers came up with such a ridiculous
                  notion.
                  According to their point, a class with static methods will load in the
                  initialization of the program, while a class without static methods
                  would only be loaded when the class is used. This, in a program with
                  many classes, would slow down the initialization.

                  Does it make sense?

                  André

                  Comment

                  • Steve Drake

                    #10
                    Re: Static methods slower?

                    It sounds like you know what you're doing and your boss does not :D.

                    good look

                    Dustin's Example is spot on.

                    If you don't need to manage state etc, then static are norms good.

                    From my experiences, I have normally see the following :

                    Facade
                    100% instance
                    some times read only calls are static.

                    Rules
                    Real code goes in instances, some helper stuff goes in static methods.

                    Data Layer
                    100% static and 100% no state.

                    The facade is debatable, we use AOP to-do achieve transactions in our facade
                    so it works best as instance, see
                    http://stevesdotnetblo g.spaces.live.c om/blog/cns!DB522AEA514 170CA!109.entry
                    For some examples on this.

                    There are good arguments, but performance is not one of them.

                    But, if you have to create an instance of the class each time just to call a
                    simple static method then it's really going to suffer. I changed Dustin's
                    example

                    I got the following numbers:

                    10,000,000 static method calls took approximately 0.002045 seconds
                    10,000,000 instance method calls took approximately 0.002036 seconds
                    10,000,000 instance method re-creating class took approximately 0.130855
                    seconds

                    Note, I used 10,000,000 calls rather than 1,000,000 as I ran a release
                    build.

                    Good luck.

                    Steve






                    "André" <andre.nho@gmai l.comwrote in message
                    news:1168446478 .838847.215610@ o58g2000hsb.goo glegroups.com.. .
                    Hello,

                    I'm having a discussion with my colleagues (and boss) over the use of
                    static methods in a class.

                    My colleagues say that static methods should be avoided whenever is
                    possible, because their use slows down the application.

                    For me, this sounds absurd - I believe that static methods should be
                    used whenever they're necessary. However, I wasn't able to find any
                    reliable documents on that. Does anyone knows of a reliable source
                    (like Microsoft) that discusses that?

                    Thank you in advance,

                    André

                    Comment

                    • Dustin Campbell

                      #11
                      Re: Static methods slower?

                      >That MVP is correct. The majority of savings is because the 'this'
                      >pointer does not need to be loaded onto the stack before calling a
                      >static method. I would love to know where these co-workers came up
                      >with such a ridiculous notion.
                      >>
                      According to their point, a class with static methods will load in the
                      initialization of the program, while a class without static methods
                      would only be loaded when the class is used. This, in a program with
                      many classes, would slow down the initialization.
                      >
                      Does it make sense?
                      They are wrong about that as well. Classes with static methods are not loaded
                      when a program is initialized. They are loaded the first time that a class
                      is used. Consider this example:

                      using System;

                      namespace StaticMethodTes t
                      {
                      class ClassWithStatic Methods
                      {
                      static int g_Data;

                      static ClassWithStatic Methods()
                      {
                      Console.WriteLi ne("ClassWithSt aticMethods loaded");

                      g_Data = 42;
                      }

                      public static int GetData()
                      {
                      return g_Data;
                      }
                      }

                      class Program
                      {
                      static void Main(string[] args)
                      {
                      Console.WriteLi ne("Main() entered");

                      Console.WriteLi ne("About to access ClassWithStatic Methods");
                      Console.WriteLi ne("ClassWithSt aticMethods.Dat a = {0}", ClassWithStatic Methods.GetData ());

                      Console.WriteLi ne("Main() exiting");
                      }
                      }
                      }

                      It outputs the following text to the console:

                      Main() entered
                      About to access ClassWithStatic Methods
                      ClassWithStatic Methods loaded
                      ClassWithStatic Methods.Data = 42
                      Main() exiting

                      Obviously, ClassWithStatic Methods was not loaded until the first time that
                      it was accessed by the application.

                      Best Regards,
                      Dustin Campbell
                      Developer Express Inc.


                      Comment

                      • Willy Denoyette [MVP]

                        #12
                        Re: Static methods slower?

                        "André" <andre.nho@gmai l.comwrote in message
                        news:1168450494 .724044.30030@k 58g2000hse.goog legroups.com...
                        That MVP is correct. The majority of savings is because the 'this' pointer
                        does not need to be loaded onto the stack before calling a static method.
                        I would love to know where these co-workers came up with such a ridiculous
                        notion.
                        According to their point, a class with static methods will load in the
                        initialization of the program, while a class without static methods
                        would only be loaded when the class is used. This, in a program with
                        many classes, would slow down the initialization.

                        Does it make sense?

                        No it doesn't, the class will be loaded at first call of a static method, NOT at program
                        initialization time.

                        Willy.

                        Comment

                        • Barry Kelly

                          #13
                          Re: Static methods slower?

                          André wrote:
                          That MVP is correct. The majority of savings is because the 'this' pointer
                          does not need to be loaded onto the stack before calling a static method.
                          I would love to know where these co-workers came up with such a ridiculous
                          notion.
                          >
                          According to their point, a class with static methods will load in the
                          initialization of the program, while a class without static methods
                          would only be loaded when the class is used. This, in a program with
                          many classes, would slow down the initialization.
                          >
                          Does it make sense?
                          No :) Types are initialized at a runtime-determined time, which is
                          usually just as the first member (static or constructor in the case of
                          instance) is accessed.

                          To be sure, write a test with class constructors etc.

                          -- Barry

                          --

                          Comment

                          • Willy Denoyette [MVP]

                            #14
                            Re: Static methods slower?

                            "Dustin Campbell" <dustinc@no-spam-pleasedevexpres s.comwrote in message
                            news:c155cb0e21 26c8c902c81bd9f 910@news.micros oft.com...
                            >>I positively agree with you. But, unfortunately, unless I provide
                            >>some reliable documentation (like Microsoft's) on that, I can't have
                            >>my point made... :-(
                            >>>
                            >I suggestion you move on then, no sense in working with people that
                            >have such absurd beliefs.
                            >>
                            >Here's one link I found that actually claims statics are faster, from
                            >an MVP..
                            >>
                            >http://forums.microsoft.com/MSDN/Sho...47372&SiteID=1
                            >
                            That MVP is correct. The majority of savings is because the 'this' pointer does not need
                            to be loaded onto the stack before calling a static method. I would love to know where
                            these co-workers came up with such a ridiculous notion.
                            >
                            Actually the this pointer is not passed on the stack, managed code used the CLR calling
                            convention when calling instance methods, here the this pointer and the first argument (if
                            any) are passed in a register.

                            Willy.

                            Comment

                            • Dustin Campbell

                              #15
                              Re: Static methods slower?

                              "Dustin Campbell" <dustinc@no-spam-pleasedevexpres s.comwrote in
                              message news:c155cb0e21 26c8c902c81bd9f 910@news.micros oft.com...
                              >
                              >>>I positively agree with you. But, unfortunately, unless I provide
                              >>>some reliable documentation (like Microsoft's) on that, I can't
                              >>>have my point made... :-(
                              >>>>
                              >>I suggestion you move on then, no sense in working with people that
                              >>have such absurd beliefs.
                              >>>
                              >>Here's one link I found that actually claims statics are faster,
                              >>from an MVP..
                              >>>
                              >>http://forums.microsoft.com/MSDN/Sho...1047372&SiteID
                              >>=1
                              >>>
                              >That MVP is correct. The majority of savings is because the 'this'
                              >pointer does not need to be loaded onto the stack before calling a
                              >static method. I would love to know where these co-workers came up
                              >with such a ridiculous notion.
                              >>
                              Actually the this pointer is not passed on the stack, managed code
                              used the CLR calling convention when calling instance methods, here
                              the this pointer and the first argument (if any) are passed in a
                              register.
                              Right. Thanks for the clarification Willy.

                              OK, so it's even *less* of a difference. :-)

                              Best Regards,
                              Dustin Campbell
                              Developer Express Inc.


                              Comment

                              Working...