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 / C++ / MFC
  4. Which is faster?

Which is faster?

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestioncssdiscussion
24 Posts 6 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.
  • A Aescleal

    The only way to find out is to write some tests, compile and run them on your system. Arguing thoretically about what's faster is fairly pointless. Cheers, Ash

    C Offline
    C Offline
    Code o mat
    wrote on last edited by
    #3

    I guess you have a point there...

    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

    1 Reply Last reply
    0
    • C Code o mat

      Hey folks! I was just wondering, which of these would be faster:

      RECT R1, R2;
      POINT Offset;
      ...
      R1 = R2;
      R1.left += Offset.x;
      R1.right += Offset.x;
      R1.top += Offset.y;
      R1.bottom += Offset.y;

      or

      RECT R1, R2;
      POINT Offset;
      ...
      R1.left = R2.left + Offset.x;
      R1.right = R2.right + Offset.x;
      R1.top = R2.top + Offset.y;
      R1.bottom = R2.bottom + Offset.y;

      As a "sub question of this", would this:

      R1.left += Offset.x;
      R1.right += Offset.x;
      R1.top += Offset.y;
      R1.bottom += Offset.y;

      be faster than this:

      R1.left += Offset.x;
      R1.top += Offset.y;
      R1.right += Offset.x;
      R1.bottom += Offset.y;

      ? I mean, in the first version when the first addition is done Offset.x is already loaded into one of the registers so the second addition needs less memory access, while in the second version, it would need to retrieve Offset.y to do the addition, then retrieve Offset.x again and then Offset.y again. In the first version it has to retrieve both Offset.x and Offset.y only once during the 4 additions, so that's half the memory access from Offset's point of view. Right? If so, I also wonder if the compiler is smart enough to change the order of these to produce a more speed optimized code (i personally highly doubt this). Thanks for any sharing of thoughts. :)

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Computers don't kill programs, users kill programs < > "It doesn't work, fix it" does not qualify as a bug report. <

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

      2 no :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

      C 1 Reply Last reply
      0
      • C Code o mat

        Hey folks! I was just wondering, which of these would be faster:

        RECT R1, R2;
        POINT Offset;
        ...
        R1 = R2;
        R1.left += Offset.x;
        R1.right += Offset.x;
        R1.top += Offset.y;
        R1.bottom += Offset.y;

        or

        RECT R1, R2;
        POINT Offset;
        ...
        R1.left = R2.left + Offset.x;
        R1.right = R2.right + Offset.x;
        R1.top = R2.top + Offset.y;
        R1.bottom = R2.bottom + Offset.y;

        As a "sub question of this", would this:

        R1.left += Offset.x;
        R1.right += Offset.x;
        R1.top += Offset.y;
        R1.bottom += Offset.y;

        be faster than this:

        R1.left += Offset.x;
        R1.top += Offset.y;
        R1.right += Offset.x;
        R1.bottom += Offset.y;

        ? I mean, in the first version when the first addition is done Offset.x is already loaded into one of the registers so the second addition needs less memory access, while in the second version, it would need to retrieve Offset.y to do the addition, then retrieve Offset.x again and then Offset.y again. In the first version it has to retrieve both Offset.x and Offset.y only once during the 4 additions, so that's half the memory access from Offset's point of view. Right? If so, I also wonder if the compiler is smart enough to change the order of these to produce a more speed optimized code (i personally highly doubt this). Thanks for any sharing of thoughts. :)

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Computers don't kill programs, users kill programs < > "It doesn't work, fix it" does not qualify as a bug report. <

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #5

        You may have a look at the assembly code (compile with /E option). :)

        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.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        C 2 Replies Last reply
        0
        • C CPallini

          You may have a look at the assembly code (compile with /E option). :)

          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.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #6

          Yeah, i thought of that too... I think i will just try to write a small proggie that test these methods and see which performs better. Thanks for your answer.

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

          D 1 Reply Last reply
          0
          • A Aescleal

            The only way to find out is to write some tests, compile and run them on your system. Arguing thoretically about what's faster is fairly pointless. Cheers, Ash

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #7

            Hey, in case you care, i wrote a little tester that uses the high performance counter to check how much it takes to do the given equations 100million times, here are the resuls, 1a being the first method, 1b being the first method with the different order and 2 being the second method: <1a> 0.698949 sec <1b> 0.750401 sec <2 > 0.527537 sec ---------- <1a> 0.711031 sec <1b> 0.701738 sec <2 > 0.508943 sec ---------- <1a> 0.699821 sec <1b> 0.710665 sec <2 > 0.514772 sec ---------- <1a> 0.695679 sec <1b> 0.693272 sec <2 > 0.510279 sec ---------- <1a> 0.697279 sec <1b> 0.696093 sec <2 > 0.569204 sec Seems like method 2 indeed wins, changing the order doesn't seem to make much of a difference (diff. between 1a and 1b). [Edit] Tests were done with a no-optimization debug build.

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

            modified on Saturday, June 19, 2010 11:59 AM

            A 1 Reply Last reply
            0
            • L Luc Pattyn

              2 no :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #8

              In case you care, here[^] are some test results, seems like you are right. :thumbsup:

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

              L 1 Reply Last reply
              0
              • C CPallini

                You may have a look at the assembly code (compile with /E option). :)

                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.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                C Offline
                C Offline
                Code o mat
                wrote on last edited by
                #9

                In case you care, i did the testing, here are[^] the results.

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                C 1 Reply Last reply
                0
                • C Code o mat

                  Yeah, i thought of that too... I think i will just try to write a small proggie that test these methods and see which performs better. Thanks for your answer.

                  > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                  D Offline
                  D Offline
                  Druuler
                  wrote on last edited by
                  #10

                  both compiled down to the same code for me. :)

                  C 1 Reply Last reply
                  0
                  • C Code o mat

                    In case you care, here[^] are some test results, seems like you are right. :thumbsup:

                    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

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

                    Thanks. I had already seen your other message. You should make clear what build you used (debug/release) and what optimization level, it may be relevant, not to the outcome, but to the relative difference. And the conclusion should be: moves are not free; try and combine them with actual calculations. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                    1 Reply Last reply
                    0
                    • D Druuler

                      both compiled down to the same code for me. :)

                      C Offline
                      C Offline
                      Code o mat
                      wrote on last edited by
                      #12

                      Try with optimizations turned off.

                      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                      D 1 Reply Last reply
                      0
                      • C Code o mat

                        Hey, in case you care, i wrote a little tester that uses the high performance counter to check how much it takes to do the given equations 100million times, here are the resuls, 1a being the first method, 1b being the first method with the different order and 2 being the second method: <1a> 0.698949 sec <1b> 0.750401 sec <2 > 0.527537 sec ---------- <1a> 0.711031 sec <1b> 0.701738 sec <2 > 0.508943 sec ---------- <1a> 0.699821 sec <1b> 0.710665 sec <2 > 0.514772 sec ---------- <1a> 0.695679 sec <1b> 0.693272 sec <2 > 0.510279 sec ---------- <1a> 0.697279 sec <1b> 0.696093 sec <2 > 0.569204 sec Seems like method 2 indeed wins, changing the order doesn't seem to make much of a difference (diff. between 1a and 1b). [Edit] Tests were done with a no-optimization debug build.

                        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                        modified on Saturday, June 19, 2010 11:59 AM

                        A Offline
                        A Offline
                        Aescleal
                        wrote on last edited by
                        #13

                        That's interesting 'cause on three compilers (VC++2010, Digital Mars latest and gcc 4.1) I got pretty much the same execution times for all three cases (the variation between runs was of the same order as the variation between methods). VC++ and Digital Mars went a bit bonkers and optimised the entire calculation away until I did some fiddling to make sure that the results were referenced by something else. Cheers, Ash

                        C 1 Reply Last reply
                        0
                        • A Aescleal

                          That's interesting 'cause on three compilers (VC++2010, Digital Mars latest and gcc 4.1) I got pretty much the same execution times for all three cases (the variation between runs was of the same order as the variation between methods). VC++ and Digital Mars went a bit bonkers and optimised the entire calculation away until I did some fiddling to make sure that the results were referenced by something else. Cheers, Ash

                          C Offline
                          C Offline
                          Code o mat
                          wrote on last edited by
                          #14

                          I used a debug build to avoid optimisations, and the compiler comes from VS2003 (i know, i know, it's old...)

                          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                          A 1 Reply Last reply
                          0
                          • C Code o mat

                            I used a debug build to avoid optimisations, and the compiler comes from VS2003 (i know, i know, it's old...)

                            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                            A Offline
                            A Offline
                            Aescleal
                            wrote on last edited by
                            #15

                            Doesn't switching the optimiser off defeat the idea of having an optimising compiler? And I wouldn't worry about using VC++ 2003. It's under 10 years old and actually supports a reasonable amount of the C++ 98 standard unlike all the numpties who painted themselves into a corner using VC++ 6 and now can't move off it because they were programming to MS's standard. Cheers, Ash

                            C C 2 Replies Last reply
                            0
                            • A Aescleal

                              Doesn't switching the optimiser off defeat the idea of having an optimising compiler? And I wouldn't worry about using VC++ 2003. It's under 10 years old and actually supports a reasonable amount of the C++ 98 standard unlike all the numpties who painted themselves into a corner using VC++ 6 and now can't move off it because they were programming to MS's standard. Cheers, Ash

                              C Offline
                              C Offline
                              Code o mat
                              wrote on last edited by
                              #16

                              As said here[^], probably the compiler modifies the code to its likes to produce the -possibly- fastest way to do it. Which kinda renders the whole question obsolete i guess, since no matter how i do it the result will be the same, or at least, which is faster will depend on the used compiler/optimization method. So i'd say, turning off optimizations is only important from the "hypothetical question's" point of view.

                              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                              1 Reply Last reply
                              0
                              • C Code o mat

                                Try with optimizations turned off.

                                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                                D Offline
                                D Offline
                                Druuler
                                wrote on last edited by
                                #17

                                they were disabled...

                                C 1 Reply Last reply
                                0
                                • D Druuler

                                  they were disabled...

                                  C Offline
                                  C Offline
                                  Code o mat
                                  wrote on last edited by
                                  #18

                                  That's odd...

                                  > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                                  1 Reply Last reply
                                  0
                                  • A Aescleal

                                    Doesn't switching the optimiser off defeat the idea of having an optimising compiler? And I wouldn't worry about using VC++ 2003. It's under 10 years old and actually supports a reasonable amount of the C++ 98 standard unlike all the numpties who painted themselves into a corner using VC++ 6 and now can't move off it because they were programming to MS's standard. Cheers, Ash

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

                                    Aescleal wrote:

                                    Doesn't switching the optimiser off defeat the idea of having an optimising compiler?

                                    And the whole point of a speed test, I would say. :)

                                    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.
                                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                    [My articles]

                                    1 Reply Last reply
                                    0
                                    • C Code o mat

                                      In case you care, i did the testing, here are[^] the results.

                                      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                                      C Offline
                                      C Offline
                                      CPallini
                                      wrote on last edited by
                                      #20

                                      Yes, I can care. Anyway I wouldn't use a debug build for a speed test. :)

                                      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.
                                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                      [My articles]

                                      C 1 Reply Last reply
                                      0
                                      • C CPallini

                                        Yes, I can care. Anyway I wouldn't use a debug build for a speed test. :)

                                        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.
                                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                        [My articles]

                                        C Offline
                                        C Offline
                                        Code o mat
                                        wrote on last edited by
                                        #21

                                        Just for you ( :) ), here are the results of a release build with "default" optimization (for speed): <1a> 0.0000022349 sec <1b> 0.0000019556 sec <2 > 0.0000019556 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000016762 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000016762 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000019556 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000019556 sec and with optimizations turned off: <1a> 0.7622823571 sec <1b> 0.7618152586 sec <2 > 0.5077494486 sec ---------- <1a> 0.7632852779 sec <1b> 0.7537491497 sec <2 > 0.5121279380 sec ---------- <1a> 0.7620644523 sec <1b> 0.7755153239 sec <2 > 0.5118524840 sec ---------- <1a> 0.7574504581 sec <1b> 0.7570453787 sec <2 > 0.5195719009 sec ---------- <1a> 0.7594526679 sec <1b> 0.7610942173 sec <2 > 0.5190137294 sec [EDIT] I wonder where the "variance" of the time needed for the 2nd method comes from with optimizations applied.

                                        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                                        C 1 Reply Last reply
                                        0
                                        • C Code o mat

                                          Just for you ( :) ), here are the results of a release build with "default" optimization (for speed): <1a> 0.0000022349 sec <1b> 0.0000019556 sec <2 > 0.0000019556 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000016762 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000016762 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000019556 sec ---------- <1a> 0.0000016762 sec <1b> 0.0000016762 sec <2 > 0.0000019556 sec and with optimizations turned off: <1a> 0.7622823571 sec <1b> 0.7618152586 sec <2 > 0.5077494486 sec ---------- <1a> 0.7632852779 sec <1b> 0.7537491497 sec <2 > 0.5121279380 sec ---------- <1a> 0.7620644523 sec <1b> 0.7755153239 sec <2 > 0.5118524840 sec ---------- <1a> 0.7574504581 sec <1b> 0.7570453787 sec <2 > 0.5195719009 sec ---------- <1a> 0.7594526679 sec <1b> 0.7610942173 sec <2 > 0.5190137294 sec [EDIT] I wonder where the "variance" of the time needed for the 2nd method comes from with optimizations applied.

                                          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

                                          C Offline
                                          C Offline
                                          CPallini
                                          wrote on last edited by
                                          #22

                                          Thank you, sir. :)

                                          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.
                                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                          [My articles]

                                          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