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. Other Discussions
  3. The Weird and The Wonderful
  4. A little tiny horror

A little tiny horror

Scheduled Pinned Locked Moved The Weird and The Wonderful
35 Posts 17 Posters 167 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.
  • G Gary Wheeler

    I found this expression in our current source code:

    int i;
    //...
    (int)pow(2,i)

    This was in code written by a senior developer :wtf:. I replaced it with the following expression:

    (1 << i)

    Software Zen: delete this;

    Q Offline
    Q Offline
    QuiJohn
    wrote on last edited by
    #2

    I'd have to see more of the code to decide if this really deserves to be a horror. It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code. I'm a much bigger fan of readable code than optimizations with negligible performance improvements. (Not that "2 << i" is that unreadable, but you get the idea.)


    Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

    C R G CPalliniC P 5 Replies Last reply
    0
    • Q QuiJohn

      I'd have to see more of the code to decide if this really deserves to be a horror. It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code. I'm a much bigger fan of readable code than optimizations with negligible performance improvements. (Not that "2 << i" is that unreadable, but you get the idea.)


      Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

      C Offline
      C Offline
      Chris Maunder
      wrote on last edited by
      #3

      Shift left is way cooler than pow any day. And in the end it's all about how good the code looks, eh? ;)

      cheers, Chris Maunder

      CodeProject.com : C++ MVP

      P P R 3 Replies Last reply
      0
      • Q QuiJohn

        I'd have to see more of the code to decide if this really deserves to be a horror. It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code. I'm a much bigger fan of readable code than optimizations with negligible performance improvements. (Not that "2 << i" is that unreadable, but you get the idea.)


        Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

        R Offline
        R Offline
        Robert Surtees
        wrote on last edited by
        #4

        David Kentley wrote:

        It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code.

        I'm pretty sure pow() only takes and returns doubles which means the original code is casting two ints in and one back out in addition to its internal pow goodness. I'm guessing the shift is a wee bit faster. :)

        J CPalliniC 2 Replies Last reply
        0
        • G Gary Wheeler

          I found this expression in our current source code:

          int i;
          //...
          (int)pow(2,i)

          This was in code written by a senior developer :wtf:. I replaced it with the following expression:

          (1 << i)

          Software Zen: delete this;

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #5

          Gary Wheeler wrote:

          I replaced it with the following expression:

          And in the process introduced a subtle bug ;P

          xacc.ide - now with IronScheme support
          IronScheme - 1.0 alpha 1 out now

          R G 2 Replies Last reply
          0
          • L leppie

            Gary Wheeler wrote:

            I replaced it with the following expression:

            And in the process introduced a subtle bug ;P

            xacc.ide - now with IronScheme support
            IronScheme - 1.0 alpha 1 out now

            R Offline
            R Offline
            Robert Rohde
            wrote on last edited by
            #6

            Good catch! I assume you are referring to the different results when having i with values smaller than 0 and bigger than 31... Robert

            L 1 Reply Last reply
            0
            • G Gary Wheeler

              I found this expression in our current source code:

              int i;
              //...
              (int)pow(2,i)

              This was in code written by a senior developer :wtf:. I replaced it with the following expression:

              (1 << i)

              Software Zen: delete this;

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #7

              The code got optimized for readability for the 'junior' programmers. And it has the potential to get easily chanced to (int)pow(3,i). I say this to defend your senior... :-O

              Greetings from Germany

              G 1 Reply Last reply
              0
              • R Robert Surtees

                David Kentley wrote:

                It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code.

                I'm pretty sure pow() only takes and returns doubles which means the original code is casting two ints in and one back out in addition to its internal pow goodness. I'm guessing the shift is a wee bit faster. :)

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #8

                Robert Surtees wrote:

                I'm guessing the shift is a wee bit faster.

                Which, as was argued here, is very probably of no importance, while the resulting obfuscation of the intent of the calculation is important.

                Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                R G 2 Replies Last reply
                0
                • Q QuiJohn

                  I'd have to see more of the code to decide if this really deserves to be a horror. It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code. I'm a much bigger fan of readable code than optimizations with negligible performance improvements. (Not that "2 << i" is that unreadable, but you get the idea.)


                  Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

                  G Offline
                  G Offline
                  Gary Wheeler
                  wrote on last edited by
                  #9

                  The original surrounding code looked something like this:

                  for (int i = 0; i < 16; i++) {
                  CString name;
                  name.Format(_T("Stuff_%d.dat"),(int)pow(2,i));
                  };

                  Thinking about it, if I wanted to maximize readability, I would have done this:

                  int name_value = 1;
                  for (int i = 0; i < 16; i++) {
                  CString name;
                  name.Format(_T("Stuff_%d.dat"),name_value);
                  name_value *= 2;
                  };

                  The only reason I found this is a compiler error in the original (int)pow(2,i) expression from VS2008 (ambiguous override).

                  Software Zen: delete this;

                  1 Reply Last reply
                  0
                  • K KarstenK

                    The code got optimized for readability for the 'junior' programmers. And it has the potential to get easily chanced to (int)pow(3,i). I say this to defend your senior... :-O

                    Greetings from Germany

                    G Offline
                    G Offline
                    Gary Wheeler
                    wrote on last edited by
                    #10

                    KarstenK wrote:

                    defend your senior...

                    Actually, I'm senior to the guy that wrote this. I just thought that the guy should have picked a better way to achieve the desired affect.

                    Software Zen: delete this;

                    L 1 Reply Last reply
                    0
                    • L leppie

                      Gary Wheeler wrote:

                      I replaced it with the following expression:

                      And in the process introduced a subtle bug ;P

                      xacc.ide - now with IronScheme support
                      IronScheme - 1.0 alpha 1 out now

                      G Offline
                      G Offline
                      Gary Wheeler
                      wrote on last edited by
                      #11

                      Actually not; see my reply above.

                      Software Zen: delete this;

                      1 Reply Last reply
                      0
                      • R Robert Rohde

                        Good catch! I assume you are referring to the different results when having i with values smaller than 0 and bigger than 31... Robert

                        L Offline
                        L Offline
                        leppie
                        wrote on last edited by
                        #12

                        Just smaller than 0, but as he is casting to int 31 or above could be a problem too, not sure if C (which I assume this is) will create a 'long' or 'long long' or whatever they use from the shift. But the result of usage of negative numbers are clear undefined.

                        xacc.ide - now with IronScheme support
                        IronScheme - 1.0 alpha 1 out now

                        1 Reply Last reply
                        0
                        • G Gary Wheeler

                          KarstenK wrote:

                          defend your senior...

                          Actually, I'm senior to the guy that wrote this. I just thought that the guy should have picked a better way to achieve the desired affect.

                          Software Zen: delete this;

                          L Offline
                          L Offline
                          leppie
                          wrote on last edited by
                          #13

                          Like:

                          int pow(int i, int j)
                          {
                          switch (j)
                          case 0: return 1;
                          case 1: return i;
                          case 2: return i * i;
                          case 3: return i * i * i;
                          case 4: return i * i * i * i;
                          ...
                          }

                          ;P

                          xacc.ide - now with IronScheme support
                          IronScheme - 1.0 alpha 1 out now

                          CPalliniC T 2 Replies Last reply
                          0
                          • L leppie

                            Like:

                            int pow(int i, int j)
                            {
                            switch (j)
                            case 0: return 1;
                            case 1: return i;
                            case 2: return i * i;
                            case 3: return i * i * i;
                            case 4: return i * i * i * i;
                            ...
                            }

                            ;P

                            xacc.ide - now with IronScheme support
                            IronScheme - 1.0 alpha 1 out now

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #14

                            Nope. You forgot break; statement. Oh pardon, you're senior! :-D

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            [my articles]

                            In testa che avete, signor di Ceprano?

                            L 1 Reply Last reply
                            0
                            • G Gary Wheeler

                              I found this expression in our current source code:

                              int i;
                              //...
                              (int)pow(2,i)

                              This was in code written by a senior developer :wtf:. I replaced it with the following expression:

                              (1 << i)

                              Software Zen: delete this;

                              CPalliniC Offline
                              CPalliniC Offline
                              CPallini
                              wrote on last edited by
                              #15

                              Gary Wheeler wrote:

                              This was in code written by a senior developer

                              When seniority approaches retirement... :-D

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              [my articles]

                              In testa che avete, signor di Ceprano?

                              1 Reply Last reply
                              0
                              • CPalliniC CPallini

                                Nope. You forgot break; statement. Oh pardon, you're senior! :-D

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                [my articles]

                                L Offline
                                L Offline
                                leppie
                                wrote on last edited by
                                #16

                                We break for nothing! :p

                                xacc.ide - now with IronScheme support
                                IronScheme - 1.0 alpha 1 out now

                                1 Reply Last reply
                                0
                                • Q QuiJohn

                                  I'd have to see more of the code to decide if this really deserves to be a horror. It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code. I'm a much bigger fan of readable code than optimizations with negligible performance improvements. (Not that "2 << i" is that unreadable, but you get the idea.)


                                  Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

                                  CPalliniC Offline
                                  CPalliniC Offline
                                  CPallini
                                  wrote on last edited by
                                  #17

                                  David Kentley wrote:

                                  Not that "2 << i" is that unreadable, but you get the idea.

                                  In fact it isn't unreadable, it is wrong. :-D

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  [my articles]

                                  In testa che avete, signor di Ceprano?

                                  1 Reply Last reply
                                  0
                                  • J jhwurmbach

                                    Robert Surtees wrote:

                                    I'm guessing the shift is a wee bit faster.

                                    Which, as was argued here, is very probably of no importance, while the resulting obfuscation of the intent of the calculation is important.

                                    Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                                    Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                                    R Offline
                                    R Offline
                                    Robert Surtees
                                    wrote on last edited by
                                    #18

                                    jhwurmbach wrote:

                                    Robert Surtees wrote: I'm guessing the shift is a wee bit faster. Which, as was argued here, is very probably of no importance, while the resulting obfuscation of the intent of the calculation is important.

                                    I'd say it's important. Gary's XT doesn't have an 8087 plugged in.

                                    1 Reply Last reply
                                    0
                                    • R Robert Surtees

                                      David Kentley wrote:

                                      It's possible that, as an optimization, "fixing" this has zero impact on real world performance while (slightly) obfuscating the code.

                                      I'm pretty sure pow() only takes and returns doubles which means the original code is casting two ints in and one back out in addition to its internal pow goodness. I'm guessing the shift is a wee bit faster. :)

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #19

                                      Robert Surtees wrote:

                                      I'm pretty sure pow() only takes and returns doubles which means the original code is casting two ints in and one back out in addition to its internal pow goodness. I'm guessing the shift is a wee bit faster.

                                      VC++ compiler is smart enough to implement it using shift instead of pow. Anyway, IMHO shift syntax is far more clean. :)

                                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                      [my articles]

                                      In testa che avete, signor di Ceprano?

                                      1 Reply Last reply
                                      0
                                      • L leppie

                                        Like:

                                        int pow(int i, int j)
                                        {
                                        switch (j)
                                        case 0: return 1;
                                        case 1: return i;
                                        case 2: return i * i;
                                        case 3: return i * i * i;
                                        case 4: return i * i * i * i;
                                        ...
                                        }

                                        ;P

                                        xacc.ide - now with IronScheme support
                                        IronScheme - 1.0 alpha 1 out now

                                        T Offline
                                        T Offline
                                        Tim Smith
                                        wrote on last edited by
                                        #20

                                        int pow(int i, int j) { switch (j) { default: return 0; case 0: return 1; case 32: i *= i; case 31: i *= i; case 30: i *= i; ... case 1: return i; } } .... ewwwww

                                        Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                        L CPalliniC 2 Replies Last reply
                                        0
                                        • T Tim Smith

                                          int pow(int i, int j) { switch (j) { default: return 0; case 0: return 1; case 32: i *= i; case 31: i *= i; case 30: i *= i; ... case 1: return i; } } .... ewwwww

                                          Tim Smith I'm going to patent thought. I have yet to see any prior art.

                                          L Offline
                                          L Offline
                                          leppie
                                          wrote on last edited by
                                          #21

                                          Even better :p

                                          xacc.ide - now with IronScheme support
                                          IronScheme - 1.0 alpha 1 out now

                                          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