Static vs Non-Static Function Performance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve - DND

    Static vs Non-Static Function Performance

    We're currently doing some tests to determine the performance of static vs
    non-static functions, and we're coming up with some odd(in our opinion)
    results. We used a very simple setup. One class had a static function, and
    the one class had a non-static function. Both of these functions did the
    exact same thing.

    The test function:
    public void Test(){
    decimal y = 2;
    decimal x = 3;
    decimal d = 0;

    for(decimal z = 0; z < 1000000;z++){
    if(z % 2 == 0){
    d = y*d;
    } else {
    d = x*d;
    }
    }
    }

    The test running code:
    for (int i = 0; i < 1000; i++) {
    StaticTest.Test ();
    }

    for (int i = 0; i < 1000; i++) {
    NonStaticTest nst = new NonStaticTest() ;
    nst.Test();
    }

    To our suprise the non-static approach ran in 15 minutes, and the static
    approach ran in 17 minutes. We had figured that due to the need to
    instantiate a new object with each call for the non-static approach, that
    this way would take longer. We also tried running the non-static approach
    first, and the static approach second, the results were the same. We ran
    these tests 3 times each to make sure that we got a nice average.

    Did we somehow bias the test towards the non-static approach? How is it that
    with an object instantion going on, the non-static approach is able to run
    in 2 minutes less time. This is of course a substantially large margin, and
    could have some definite performance implications for those building
    performance oriented code.

    Thanks,
    Steve


  • Michael Culley

    #2
    Re: Static vs Non-Static Function Performance

    Hi Steve,

    There could be some optimisations going on that are giving false results.

    --
    Michael Culley


    "Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#wYIHS8pDH A.1444@tk2msftn gp13.phx.gbl...[color=blue]
    > We're currently doing some tests to determine the performance of static vs
    > non-static functions, and we're coming up with some odd(in our opinion)
    > results. We used a very simple setup. One class had a static function, and
    > the one class had a non-static function. Both of these functions did the
    > exact same thing.
    >
    > The test function:
    > public void Test(){
    > decimal y = 2;
    > decimal x = 3;
    > decimal d = 0;
    >
    > for(decimal z = 0; z < 1000000;z++){
    > if(z % 2 == 0){
    > d = y*d;
    > } else {
    > d = x*d;
    > }
    > }
    > }
    >
    > The test running code:
    > for (int i = 0; i < 1000; i++) {
    > StaticTest.Test ();
    > }
    >
    > for (int i = 0; i < 1000; i++) {
    > NonStaticTest nst = new NonStaticTest() ;
    > nst.Test();
    > }
    >
    > To our suprise the non-static approach ran in 15 minutes, and the static
    > approach ran in 17 minutes. We had figured that due to the need to
    > instantiate a new object with each call for the non-static approach, that
    > this way would take longer. We also tried running the non-static approach
    > first, and the static approach second, the results were the same. We ran
    > these tests 3 times each to make sure that we got a nice average.
    >
    > Did we somehow bias the test towards the non-static approach? How is it that
    > with an object instantion going on, the non-static approach is able to run
    > in 2 minutes less time. This is of course a substantially large margin, and
    > could have some definite performance implications for those building
    > performance oriented code.
    >
    > Thanks,
    > Steve
    >
    >[/color]


    Comment

    • Steve - DND

      #3
      Re: Static vs Non-Static Function Performance

      "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
      news:O%23d7Gx8p DHA.2500@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Hi Steve,
      >
      > There could be some optimisations going on that are giving false results.
      >
      > --
      > Michael Culley[/color]

      Any idea what these enhancements could be? They should be doing the same
      thing, should they not?

      Steve


      Comment

      • Michael Culley

        #4
        Re: Static vs Non-Static Function Performance

        "Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#q9Fu18pDH A.1096@TK2MSFTN GP11.phx.gbl...[color=blue]
        > Any idea what these enhancements could be? They should be doing the same
        > thing, should they not?[/color]

        I can see several posibilities. The compiler might work out that nothing is returned from the function so optimise out some of the
        code in the function. It could treat the function as static because it doesn't use an module level variables and it might not even
        bother creating the object at all. All of these are just guesses though.

        --
        Michael Culley


        "Steve - DND" <ng@digitalnoth ing.com> wrote in message news:#q9Fu18pDH A.1096@TK2MSFTN GP11.phx.gbl...[color=blue]
        > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
        > news:O%23d7Gx8p DHA.2500@TK2MSF TNGP10.phx.gbl. ..[color=green]
        > > Hi Steve,
        > >
        > > There could be some optimisations going on that are giving false results.
        > >
        > > --
        > > Michael Culley[/color]
        >
        > Any idea what these enhancements could be? They should be doing the same
        > thing, should they not?
        >
        > Steve
        >
        >[/color]


        Comment

        • codymanix

          #5
          Re: Static vs Non-Static Function Performance

          Thats because static methods are using locks to be Thread-safe. The always
          do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.

          --
          cody

          [Freeware, Games and Humor]
          www.deutronium.de.vu || www.deutronium.tk


          Comment

          • Steve - DND

            #6
            Re: Static vs Non-Static Function Performance

            "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
            news:ungshF9pDH A.2512@TK2MSFTN GP09.phx.gbl...[color=blue]
            > "Steve - DND" <ng@digitalnoth ing.com> wrote in message[/color]
            news:#q9Fu18pDH A.1096@TK2MSFTN GP11.phx.gbl...[color=blue][color=green]
            > > Any idea what these enhancements could be? They should be doing the same
            > > thing, should they not?[/color]
            >
            > I can see several posibilities. The compiler might work out that nothing[/color]
            is returned from the function so optimise out some of the[color=blue]
            > code in the function. It could treat the function as static because it[/color]
            doesn't use an module level variables and it might not even[color=blue]
            > bother creating the object at all. All of these are just guesses though.[/color]

            Looks like you were correct. There seemed to be some optimizations going on.
            When I changed things up a bit, I got *very* different results(static
            running in less than 50% of the time).

            Thanks,
            Steve


            Comment

            • Telmo Sampaio

              #7
              Re: Static vs Non-Static Function Performance

              maybe object pooling ?

              --
              Telmo Sampaio
              MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT,
              MSF Practitioner, ITIL Certified
              telmo_sampaio@h otmail.com
              "Steve - DND" <ng@digitalnoth ing.com> wrote in message
              news:#q9Fu18pDH A.1096@TK2MSFTN GP11.phx.gbl...[color=blue]
              > "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message
              > news:O%23d7Gx8p DHA.2500@TK2MSF TNGP10.phx.gbl. ..[color=green]
              > > Hi Steve,
              > >
              > > There could be some optimisations going on that are giving false[/color][/color]
              results.[color=blue][color=green]
              > >
              > > --
              > > Michael Culley[/color]
              >
              > Any idea what these enhancements could be? They should be doing the same
              > thing, should they not?
              >
              > Steve
              >
              >[/color]


              Comment

              • mikeb

                #8
                Re: Static vs Non-Static Function Performance

                codymanix wrote:[color=blue]
                > Thats because static methods are using locks to be Thread-safe. The always
                > do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.
                >[/color]

                Are you sure? Do you have a pointer to some docs that describe this?

                I find it hard to believe that it would do this automatically, since
                it's easy enough to place the entire method body in a lock statement
                block, and there are many cases where static methods would have no need
                to use Monitors to ensure thread safety.

                --
                mikeb

                Comment

                • Alvin Bruney

                  #9
                  Re: Static vs Non-Static Function Performance

                  He is right. Take a look at the Il code or google for it.

                  --


                  -----------
                  Got TidBits?
                  Get it here: www.networkip.net/tidbits
                  "mikeb" <mailbox.google @mailnull.com> wrote in message
                  news:u7zOa09pDH A.2232@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > codymanix wrote:[color=green]
                  > > Thats because static methods are using locks to be Thread-safe. The[/color][/color]
                  always[color=blue][color=green]
                  > > do internally a Monitor.Enter() and Monitor.exit() to ensure[/color][/color]
                  Thread-safety.[color=blue][color=green]
                  > >[/color]
                  >
                  > Are you sure? Do you have a pointer to some docs that describe this?
                  >
                  > I find it hard to believe that it would do this automatically, since
                  > it's easy enough to place the entire method body in a lock statement
                  > block, and there are many cases where static methods would have no need
                  > to use Monitors to ensure thread safety.
                  >
                  > --
                  > mikeb
                  >[/color]


                  Comment

                  • Michael Culley

                    #10
                    Re: Static vs Non-Static Function Performance

                    "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in message news:OMy2Q$$pDH A.3844@tk2msftn gp13.phx.gbl...[color=blue]
                    > He is right. Take a look at the Il code or google for it.[/color]
                    So static functions should be avoided where optimum speed is needed?

                    --
                    Michael Culley



                    Comment

                    • Steve - DND

                      #11
                      Re: Static vs Non-Static Function Performance

                      > > Are you sure? Do you have a pointer to some docs that describe this?[color=blue][color=green]
                      > >
                      > > I find it hard to believe that it would do this automatically, since
                      > > it's easy enough to place the entire method body in a lock statement
                      > > block, and there are many cases where static methods would have no need
                      > > to use Monitors to ensure thread safety.[/color][/color]

                      "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                      message news:OMy2Q$$pDH A.3844@tk2msftn gp13.phx.gbl...[color=blue]
                      > He is right. Take a look at the Il code or google for it.[/color]

                      I took a look at the IL code and disassembled code for both a static and
                      non-static function, and they're identical. Can you provide an example where
                      Monitor.Enter() or Monitor.Exit() is used automatically?

                      public class StaticTest {
                      public static void Test() {
                      decimal d = 2;
                      }
                      }

                      public class NonStaticTest {
                      public void Test() {
                      decimal d = 2;
                      }
                      }

                      ***** Disassembly *****
                      ..method public hidebysig instance void Test() cil managed
                      {
                      // Code size 8 (0x8)
                      .maxstack 2
                      .locals init ([0] valuetype [mscorlib]System.Decimal d)
                      IL_0000: ldc.i4.2
                      IL_0001: newobj instance void [mscorlib]System.Decimal: :.ctor(int32)
                      IL_0006: stloc.0
                      IL_0007: ret
                      } // end of method NonStaticTest:: Test



                      ..method public hidebysig static void Test() cil managed
                      {
                      // Code size 8 (0x8)
                      .maxstack 2
                      .locals init ([0] valuetype [mscorlib]System.Decimal d)
                      IL_0000: ldc.i4.2
                      IL_0001: newobj instance void [mscorlib]System.Decimal: :.ctor(int32)
                      IL_0006: stloc.0
                      IL_0007: ret
                      } // end of method StaticTest::Tes t

                      Steve


                      Comment

                      • Alvin Bruney

                        #12
                        Re: Static vs Non-Static Function Performance

                        I think i am wrong on this one. The dissambly is painfully obvious that I
                        don't know what I am talking about :-)

                        --


                        -----------
                        Got TidBits?
                        Get it here: www.networkip.net/tidbits
                        "Steve - DND" <steve!@!digita lnothing.com> wrote in message
                        news:OdOVEMAqDH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue][color=green][color=darkred]
                        > > > Are you sure? Do you have a pointer to some docs that describe this?
                        > > >
                        > > > I find it hard to believe that it would do this automatically, since
                        > > > it's easy enough to place the entire method body in a lock statement
                        > > > block, and there are many cases where static methods would have no[/color][/color][/color]
                        need[color=blue][color=green][color=darkred]
                        > > > to use Monitors to ensure thread safety.[/color][/color]
                        >
                        > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                        > message news:OMy2Q$$pDH A.3844@tk2msftn gp13.phx.gbl...[color=green]
                        > > He is right. Take a look at the Il code or google for it.[/color]
                        >
                        > I took a look at the IL code and disassembled code for both a static and
                        > non-static function, and they're identical. Can you provide an example[/color]
                        where[color=blue]
                        > Monitor.Enter() or Monitor.Exit() is used automatically?
                        >
                        > public class StaticTest {
                        > public static void Test() {
                        > decimal d = 2;
                        > }
                        > }
                        >
                        > public class NonStaticTest {
                        > public void Test() {
                        > decimal d = 2;
                        > }
                        > }
                        >
                        > ***** Disassembly *****
                        > .method public hidebysig instance void Test() cil managed
                        > {
                        > // Code size 8 (0x8)
                        > .maxstack 2
                        > .locals init ([0] valuetype [mscorlib]System.Decimal d)
                        > IL_0000: ldc.i4.2
                        > IL_0001: newobj instance void[/color]
                        [mscorlib]System.Decimal: :.ctor(int32)[color=blue]
                        > IL_0006: stloc.0
                        > IL_0007: ret
                        > } // end of method NonStaticTest:: Test
                        >
                        >
                        >
                        > .method public hidebysig static void Test() cil managed
                        > {
                        > // Code size 8 (0x8)
                        > .maxstack 2
                        > .locals init ([0] valuetype [mscorlib]System.Decimal d)
                        > IL_0000: ldc.i4.2
                        > IL_0001: newobj instance void[/color]
                        [mscorlib]System.Decimal: :.ctor(int32)[color=blue]
                        > IL_0006: stloc.0
                        > IL_0007: ret
                        > } // end of method StaticTest::Tes t
                        >
                        > Steve
                        >
                        >[/color]


                        Comment

                        • mikeb

                          #13
                          Re: Static vs Non-Static Function Performance

                          "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in message news:<OMy2Q$$pD HA.3844@tk2msft ngp13.phx.gbl>. ..[color=blue]
                          > He is right. Take a look at the Il code or google for it.
                          >[/color]

                          Like Steve at DND, I did try this, and found no evidence of a simple
                          static method being automatically locked.
                          [color=blue]
                          >
                          >
                          > -----------
                          > Got TidBits?
                          > Get it here: www.networkip.net/tidbits
                          > "mikeb" <mailbox.google @mailnull.com> wrote in message
                          > news:u7zOa09pDH A.2232@TK2MSFTN GP09.phx.gbl...[color=green]
                          > > codymanix wrote:[color=darkred]
                          > > > Thats because static methods are using locks to be Thread-safe. The[/color][/color]
                          > always[color=green][color=darkred]
                          > > > do internally a Monitor.Enter() and Monitor.exit() to ensure[/color][/color]
                          > Thread-safety.[color=green][color=darkred]
                          > > >[/color]
                          > >
                          > > Are you sure? Do you have a pointer to some docs that describe this?
                          > >
                          > > I find it hard to believe that it would do this automatically, since
                          > > it's easy enough to place the entire method body in a lock statement
                          > > block, and there are many cases where static methods would have no need
                          > > to use Monitors to ensure thread safety.
                          > >
                          > > --
                          > > mikeb
                          > >[/color][/color]

                          --
                          mikeb

                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Static vs Non-Static Function Performance

                            codymanix <dont.spam.me.d eutronium@gmx.d e> wrote:[color=blue]
                            > Thats because static methods are using locks to be Thread-safe. The always
                            > do internally a Monitor.Enter() and Monitor.exit() to ensure Thread-safety.[/color]

                            They definitely, definitely don't.

                            Here's an example program which shows two threads being in the same
                            method at the same time:

                            using System;
                            using System.Threadin g;

                            public class Test
                            {
                            static void Main()
                            {
                            Thread t1 = new Thread (new ThreadStart(Cou nt));
                            Thread t2 = new Thread (new ThreadStart(Cou nt));

                            t1.Name = "First thread";
                            t2.Name = "Second thread";

                            t1.Start();
                            t2.Start();
                            }

                            static void Count()
                            {
                            for (int i=0; i < 10; i++)
                            {
                            Console.WriteLi ne ("{0} {1}", i,
                            Thread.CurrentT hread.Name);
                            Thread.Sleep (500);
                            }
                            }
                            }

                            If you put a lock in yourself, eg lock (typeof(Test)) round the body of
                            Count, you get radically different results (i.e. one thread does the
                            whole count, then the second one does).

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                            If replying to the group, please do not mail me too

                            Comment

                            • Stu Smith

                              #15
                              Re: Static vs Non-Static Function Performance

                              Does anyone know where this urban myth about static methods got started?


                              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                              news:MPG.1a1aab 406386d401989a7 1@msnews.micros oft.com...[color=blue]
                              > codymanix <dont.spam.me.d eutronium@gmx.d e> wrote:[color=green]
                              > > Thats because static methods are using locks to be Thread-safe. The[/color][/color]
                              always[color=blue][color=green]
                              > > do internally a Monitor.Enter() and Monitor.exit() to ensure[/color][/color]
                              Thread-safety.[color=blue]
                              >
                              > They definitely, definitely don't.
                              >
                              > Here's an example program which shows two threads being in the same
                              > method at the same time:
                              >
                              > using System;
                              > using System.Threadin g;
                              >
                              > public class Test
                              > {
                              > static void Main()
                              > {
                              > Thread t1 = new Thread (new ThreadStart(Cou nt));
                              > Thread t2 = new Thread (new ThreadStart(Cou nt));
                              >
                              > t1.Name = "First thread";
                              > t2.Name = "Second thread";
                              >
                              > t1.Start();
                              > t2.Start();
                              > }
                              >
                              > static void Count()
                              > {
                              > for (int i=0; i < 10; i++)
                              > {
                              > Console.WriteLi ne ("{0} {1}", i,
                              > Thread.CurrentT hread.Name);
                              > Thread.Sleep (500);
                              > }
                              > }
                              > }
                              >
                              > If you put a lock in yourself, eg lock (typeof(Test)) round the body of
                              > Count, you get radically different results (i.e. one thread does the
                              > whole count, then the second one does).
                              >
                              > --
                              > Jon Skeet - <skeet@pobox.co m>
                              > http://www.pobox.com/~skeet
                              > If replying to the group, please do not mail me too[/color]


                              Comment

                              Working...