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. The Lounge
  3. blind micro optimisation

blind micro optimisation

Scheduled Pinned Locked Moved The Lounge
csharpcomquestionannouncementcode-review
22 Posts 13 Posters 3 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 Super Lloyd

    on a code review someone commented I was enumerating twice a really big list, could I have only one loop instead, they said initial code, where source is a large list

    list.AddRange(source.OfType<A>());
    DoThings(source.OfType<B>());

    void DoThings(IEnumerable<B> items)
    {
    foreach (var b in item)
    {
    // do something
    }
    }

    so to please the crowd I refactored new code

    foreach (var item in source)
    {
    if (item is A a)
    list.Add(a);
    if (item is B b)
    DoThing(b);
    }
    void DoThings(IEnumerable<B> items) // still in use through an other codepath
    {
    foreach (var b in item)
    DoThing(b);
    }
    void DoThing(B b)
    {
    // do something
    }

    now... without doing any measurement (hence the blind adjective) I am suspecting version 2 is in fact slower, since enumerating is cheap, but there a lot more method calls. What says you?

    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #11

    I would measure them to be sure. But I agree that calling DoThings once is likely to be better than calling DoThing a great many times. The particular micro-optimization I'm working on now will likely lead to a post in Weird and Wonderful.

    S 1 Reply Last reply
    0
    • S Super Lloyd

      on a code review someone commented I was enumerating twice a really big list, could I have only one loop instead, they said initial code, where source is a large list

      list.AddRange(source.OfType<A>());
      DoThings(source.OfType<B>());

      void DoThings(IEnumerable<B> items)
      {
      foreach (var b in item)
      {
      // do something
      }
      }

      so to please the crowd I refactored new code

      foreach (var item in source)
      {
      if (item is A a)
      list.Add(a);
      if (item is B b)
      DoThing(b);
      }
      void DoThings(IEnumerable<B> items) // still in use through an other codepath
      {
      foreach (var b in item)
      DoThing(b);
      }
      void DoThing(B b)
      {
      // do something
      }

      now... without doing any measurement (hence the blind adjective) I am suspecting version 2 is in fact slower, since enumerating is cheap, but there a lot more method calls. What says you?

      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

      honey the codewitchH Offline
      honey the codewitchH Offline
      honey the codewitch
      wrote on last edited by
      #12

      I'm guessing the refactor is faster since it only has to enum once.

      To err is human. Fortune favors the monsters.

      S 1 Reply Last reply
      0
      • S Super Lloyd

        on a code review someone commented I was enumerating twice a really big list, could I have only one loop instead, they said initial code, where source is a large list

        list.AddRange(source.OfType<A>());
        DoThings(source.OfType<B>());

        void DoThings(IEnumerable<B> items)
        {
        foreach (var b in item)
        {
        // do something
        }
        }

        so to please the crowd I refactored new code

        foreach (var item in source)
        {
        if (item is A a)
        list.Add(a);
        if (item is B b)
        DoThing(b);
        }
        void DoThings(IEnumerable<B> items) // still in use through an other codepath
        {
        foreach (var b in item)
        DoThing(b);
        }
        void DoThing(B b)
        {
        // do something
        }

        now... without doing any measurement (hence the blind adjective) I am suspecting version 2 is in fact slower, since enumerating is cheap, but there a lot more method calls. What says you?

        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

        O Offline
        O Offline
        obermd
        wrote on last edited by
        #13

        I'd argue that you really need two lists, one of Type[A] and one of Type[B] (sorry, couldn't figure out how to avoid font changes with the <>). This appears to really be an issue of delaying the decision until too deep in the code's decision trees. The new code is easier to read and it really points out that your list should be two lists.

        1 Reply Last reply
        0
        • S Super Lloyd

          on a code review someone commented I was enumerating twice a really big list, could I have only one loop instead, they said initial code, where source is a large list

          list.AddRange(source.OfType<A>());
          DoThings(source.OfType<B>());

          void DoThings(IEnumerable<B> items)
          {
          foreach (var b in item)
          {
          // do something
          }
          }

          so to please the crowd I refactored new code

          foreach (var item in source)
          {
          if (item is A a)
          list.Add(a);
          if (item is B b)
          DoThing(b);
          }
          void DoThings(IEnumerable<B> items) // still in use through an other codepath
          {
          foreach (var b in item)
          DoThing(b);
          }
          void DoThing(B b)
          {
          // do something
          }

          now... without doing any measurement (hence the blind adjective) I am suspecting version 2 is in fact slower, since enumerating is cheap, but there a lot more method calls. What says you?

          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #14

          To me, this is a clear case of premature optimization and (almost) every optimization is premature if it sacrifices clarity when the need for optimization has not be demonstrated. There are certain cases involving function calls that I know for a fact can be done better so I deal with those up front rather than later since the delta time spent is zero. Here's an exmple : x*x is considerably faster than pow(x,2) so I deal with that up front instead of returning to it later. Especially since calling the pow function does not enhance clarity.

          "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

          1 Reply Last reply
          0
          • S Super Lloyd

            on a code review someone commented I was enumerating twice a really big list, could I have only one loop instead, they said initial code, where source is a large list

            list.AddRange(source.OfType<A>());
            DoThings(source.OfType<B>());

            void DoThings(IEnumerable<B> items)
            {
            foreach (var b in item)
            {
            // do something
            }
            }

            so to please the crowd I refactored new code

            foreach (var item in source)
            {
            if (item is A a)
            list.Add(a);
            if (item is B b)
            DoThing(b);
            }
            void DoThings(IEnumerable<B> items) // still in use through an other codepath
            {
            foreach (var b in item)
            DoThing(b);
            }
            void DoThing(B b)
            {
            // do something
            }

            now... without doing any measurement (hence the blind adjective) I am suspecting version 2 is in fact slower, since enumerating is cheap, but there a lot more method calls. What says you?

            A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

            J Offline
            J Offline
            jmaida
            wrote on last edited by
            #15

            I am not C# programmer, but I would prefer it was written in C. Overhead is easier to spot.

            "A little time, a little trouble, your better day" Badfinger

            1 Reply Last reply
            0
            • honey the codewitchH honey the codewitch

              I'm guessing the refactor is faster since it only has to enum once.

              To err is human. Fortune favors the monsters.

              S Offline
              S Offline
              Super Lloyd
              wrote on last edited by
              #16

              but it call a method for every item..... I was unsure, or maybe thought it was going to be a bit in fact slower (extra method call, where enumeration is cheap)... Anyway, I was so annoyed I made a simple test program at home and.. turns out new method is faster indeed (by 20% on my simple test thing) After that, well, I beautify the new code a bit and rolled with it! :)

              A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

              O honey the codewitchH 2 Replies Last reply
              0
              • P PIEBALDconsult

                I would measure them to be sure. But I agree that calling DoThings once is likely to be better than calling DoThing a great many times. The particular micro-optimization I'm working on now will likely lead to a post in Weird and Wonderful.

                S Offline
                S Offline
                Super Lloyd
                wrote on last edited by
                #17

                well.. I did think that too.... then I did a simple test home and, well, to my stunned surprise the optimisation was 20% faster!

                A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                1 Reply Last reply
                0
                • S Super Lloyd

                  but it call a method for every item..... I was unsure, or maybe thought it was going to be a bit in fact slower (extra method call, where enumeration is cheap)... Anyway, I was so annoyed I made a simple test program at home and.. turns out new method is faster indeed (by 20% on my simple test thing) After that, well, I beautify the new code a bit and rolled with it! :)

                  A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                  O Offline
                  O Offline
                  obermd
                  wrote on last edited by
                  #18

                  Looking at the code I would expect the second to be faster. The reason is that enumeration is slow relative to function calls. The original code enumerates three times over source and creates temporary objects. The rewritten code enumerates once over source and creates no temporary objects. Super Lloyd actually tested and found the second code is 20% faster. [The Lounge](https://www.codeproject.com/Lounge.aspx?msg=5880319#xx5880319xx)

                  1 Reply Last reply
                  0
                  • S Super Lloyd

                    on a code review someone commented I was enumerating twice a really big list, could I have only one loop instead, they said initial code, where source is a large list

                    list.AddRange(source.OfType<A>());
                    DoThings(source.OfType<B>());

                    void DoThings(IEnumerable<B> items)
                    {
                    foreach (var b in item)
                    {
                    // do something
                    }
                    }

                    so to please the crowd I refactored new code

                    foreach (var item in source)
                    {
                    if (item is A a)
                    list.Add(a);
                    if (item is B b)
                    DoThing(b);
                    }
                    void DoThings(IEnumerable<B> items) // still in use through an other codepath
                    {
                    foreach (var b in item)
                    DoThing(b);
                    }
                    void DoThing(B b)
                    {
                    // do something
                    }

                    now... without doing any measurement (hence the blind adjective) I am suspecting version 2 is in fact slower, since enumerating is cheap, but there a lot more method calls. What says you?

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    S Offline
                    S Offline
                    Sasa Cetkovic
                    wrote on last edited by
                    #19

                    Performance is not the main reason to avoid multiple enumeration. Depending on the origin of the source, the collection may be changed between any two enumerations, so the result will be a bug that is extremely hard to reproduce.

                    1 Reply Last reply
                    0
                    • S Super Lloyd

                      but it call a method for every item..... I was unsure, or maybe thought it was going to be a bit in fact slower (extra method call, where enumeration is cheap)... Anyway, I was so annoyed I made a simple test program at home and.. turns out new method is faster indeed (by 20% on my simple test thing) After that, well, I beautify the new code a bit and rolled with it! :)

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                      honey the codewitchH Offline
                      honey the codewitchH Offline
                      honey the codewitch
                      wrote on last edited by
                      #20

                      In my experience with codegen method calls don't require much overhead. I haven't run any hard tests directly, but I have written a codegen system that did method calls to separate the non-terminals and one that did not. The one that did was significantly faster, I suspect due to keeping repeatedly used code cached at the CPU level whereas with longer methods that's not possible. That's what leads me to lean toward what I leaned toward. I suspect JIT magic to reduce overhead, but there's not much you can do to with JIT to avoid allocation overhead. With .NET allocation is cheap in theory, but people forget about the overhead of the constructors.

                      To err is human. Fortune favors the monsters.

                      S 1 Reply Last reply
                      0
                      • honey the codewitchH honey the codewitch

                        In my experience with codegen method calls don't require much overhead. I haven't run any hard tests directly, but I have written a codegen system that did method calls to separate the non-terminals and one that did not. The one that did was significantly faster, I suspect due to keeping repeatedly used code cached at the CPU level whereas with longer methods that's not possible. That's what leads me to lean toward what I leaned toward. I suspect JIT magic to reduce overhead, but there's not much you can do to with JIT to avoid allocation overhead. With .NET allocation is cheap in theory, but people forget about the overhead of the constructors.

                        To err is human. Fortune favors the monsters.

                        S Offline
                        S Offline
                        Super Lloyd
                        wrote on last edited by
                        #21

                        nice hindsight! :)

                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                        honey the codewitchH 1 Reply Last reply
                        0
                        • S Super Lloyd

                          nice hindsight! :)

                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                          honey the codewitchH Offline
                          honey the codewitchH Offline
                          honey the codewitch
                          wrote on last edited by
                          #22

                          You know what you might try, if the container is indexable (your code is way up top of the thread and I don't recall it offhand) is do the loops without the enumerator.

                          for(int i = 0; i < container.Count; ++i) { ...

                          Like that. My bet is your results will be more similar at that point, but if i had to guess which one would be faster, I still suspect the method call one will be faster due to the overhead of running loops.

                          To err is human. Fortune favors the monsters.

                          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