Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Speed Optimization of C++ console application
Forum Updated to NodeBB v4.3 + New Features

Speed Optimization of C++ console application

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 10 Posters 5.4k Views 5 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #13

    @dooley said in Speed Optimization of C++ console application:

    civil engineer

    Ah, a "target" maker. Welcome aboard!

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • beeckscheB Offline
      beeckscheB Offline
      beecksche
      wrote on last edited by beecksche
      #14

      When building in Release mode, the optimization flag -O2 should be set by default.

      If speed is an issue and using lots of loops you can also enable the logs for Auto-Vectorization:

      QMAKE_CXXFLAGS_RELEASE += -Qvec-report:2
      

      Then all vectorized and non-vectorized loops will be logged and you can see where loops can be imporved. For some loops it's neccessary to set

      QMAKE_CXXFLAGS_RELEASE += -fp:fast
      

      to be vectorized. But be careful with that!

      Another way to speed up the program is to set the -Qpar flag for Auto-Parallelization:

      QMAKE_CXXFLAGS_RELEASE += -Qpar
      

      Then, if possible, loops will be parallelized. There is also an log flag for that -QPar-report:2.

      Please note, only valid for MSVC compiler and CPU architure with SSE2, AVX, and AVX2.

      kshegunovK 1 Reply Last reply
      4
      • beeckscheB beecksche

        When building in Release mode, the optimization flag -O2 should be set by default.

        If speed is an issue and using lots of loops you can also enable the logs for Auto-Vectorization:

        QMAKE_CXXFLAGS_RELEASE += -Qvec-report:2
        

        Then all vectorized and non-vectorized loops will be logged and you can see where loops can be imporved. For some loops it's neccessary to set

        QMAKE_CXXFLAGS_RELEASE += -fp:fast
        

        to be vectorized. But be careful with that!

        Another way to speed up the program is to set the -Qpar flag for Auto-Parallelization:

        QMAKE_CXXFLAGS_RELEASE += -Qpar
        

        Then, if possible, loops will be parallelized. There is also an log flag for that -QPar-report:2.

        Please note, only valid for MSVC compiler and CPU architure with SSE2, AVX, and AVX2.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #15

        @beecksche said in Speed Optimization of C++ console application:

        QMAKE_CXXFLAGS_RELEASE += -fp:fast
        

        Don't use this unless you really, really, really (and I can't emphasize that enough) know what you're doing (which is almost never). This can break promises made by the IEEE FP standard in regards to behavior and optimize out expressions that are not to be optimized. It can break proper rounding and error propagation, and floating point exceptions' diagnostics.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        5
        • Kent-DorfmanK Online
          Kent-DorfmanK Online
          Kent-Dorfman
          wrote on last edited by
          #16

          A code slowdown of a factor of 10 wouldn't be normal just with an optimization flag of -O0 vs -O2. Something else is going on here. The OP only states C++ in VS2017...No mention of CLR or native code generation in VS. Actually I'd expect the converse of the reported behaviour, where the native C++ QT runs faster if the VS C++ code is done as CLR and not native. If I had to WAG, I'd guess that the VS code is taking advantage of a .net library optimization that isn't present in native C++ QT. Without seeing the algorithms and the library links it's hard to know what exactly is going on. Heap managed memory could also play a large part in the time differences being reported.

          kshegunovK 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #17

            @Kent-Dorfman said in Speed Optimization of C++ console application:

            A code slowdown of a factor of 10 wouldn't be normal just with an optimization flag of -O0 vs -O2.

            Why not? Did you see the code? Maybe there are lots of asserts in there or other stuff... without code it's just wild guessing.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • Kent-DorfmanK Kent-Dorfman

              A code slowdown of a factor of 10 wouldn't be normal just with an optimization flag of -O0 vs -O2. Something else is going on here. The OP only states C++ in VS2017...No mention of CLR or native code generation in VS. Actually I'd expect the converse of the reported behaviour, where the native C++ QT runs faster if the VS C++ code is done as CLR and not native. If I had to WAG, I'd guess that the VS code is taking advantage of a .net library optimization that isn't present in native C++ QT. Without seeing the algorithms and the library links it's hard to know what exactly is going on. Heap managed memory could also play a large part in the time differences being reported.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #18

              @Kent-Dorfman said in Speed Optimization of C++ console application:

              A code slowdown of a factor of 10 wouldn't be normal just with an optimization flag of -O0 vs -O2.

              Actually it can be pretty normal. I've at least two rather small codebases that exhibit such speedups between debug and release (i.e. -g -O0 vs -O2). There's nothing odd about it because debug mode represents what you wrote faithfully, which isn't at all true for release builds.

              Something else is going on here.

              Not necessarily. Depends on the type of code. If you have code with a lot of templates for example the debug build is going to put a call instruction on every function call and do the regular push, pop on the stack. When the optimizer runs almost, to all, of this gets stripped down and the code is inlined, to an extreme degree. So yes, 10 time speedup between debug and release is nothing to be suspicious about.

              Read and abide by the Qt Code of Conduct

              Kent-DorfmanK 1 Reply Last reply
              3
              • D dooley

                @aha_1980 After having taken your and @aha_1980 direction on the annotation and rebuilding it appears to be working.

                Thanks to everyone for the help. I am a civil engineer putting together some simple apps for calculations I use regularly more for fun and the interest in programming than anything else, so I don't know a whole lot and can use as much help as I can get.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #19

                @dooley
                What the others are saying about optimization vs debug is probably correct, you can be surprised by how much difference it can make depending.

                However, if you are sure about your compiler flags etc. but are still stumped by speed behaviour, it may be time to compile/link for profiling your application. Both gcc & msvc have profiling (unless the free msvc does not, I don't know). This does take a bit of reading first time to set up and interpret output, but well worth it if you wish to investigate speed/performance over time in future.

                1 Reply Last reply
                2
                • kshegunovK kshegunov

                  @Kent-Dorfman said in Speed Optimization of C++ console application:

                  A code slowdown of a factor of 10 wouldn't be normal just with an optimization flag of -O0 vs -O2.

                  Actually it can be pretty normal. I've at least two rather small codebases that exhibit such speedups between debug and release (i.e. -g -O0 vs -O2). There's nothing odd about it because debug mode represents what you wrote faithfully, which isn't at all true for release builds.

                  Something else is going on here.

                  Not necessarily. Depends on the type of code. If you have code with a lot of templates for example the debug build is going to put a call instruction on every function call and do the regular push, pop on the stack. When the optimizer runs almost, to all, of this gets stripped down and the code is inlined, to an extreme degree. So yes, 10 time speedup between debug and release is nothing to be suspicious about.

                  Kent-DorfmanK Online
                  Kent-DorfmanK Online
                  Kent-Dorfman
                  wrote on last edited by
                  #20

                  @kshegunov I wrote absolutely nothing about "-g". I still maintain that simple -O0 vs -O2 is NOT going to divide performance by a factor of 10. I cannot begin to imagine how badly a person would have to design their algorithm to validate that level of performance hit. something other than compiler optimization is causing his hit...

                  kshegunovK aha_1980A 2 Replies Last reply
                  0
                  • Kent-DorfmanK Kent-Dorfman

                    @kshegunov I wrote absolutely nothing about "-g". I still maintain that simple -O0 vs -O2 is NOT going to divide performance by a factor of 10. I cannot begin to imagine how badly a person would have to design their algorithm to validate that level of performance hit. something other than compiler optimization is causing his hit...

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #21

                    @Kent-Dorfman said in Speed Optimization of C++ console application:

                    @kshegunov I wrote absolutely nothing about "-g".

                    Fair enough.

                    I still maintain that simple -O0 vs -O2 is NOT going to divide performance by a factor of 10. I cannot begin to imagine how badly a person would have to design their algorithm to validate that level of performance hit.

                    https://bitbucket.org/kshegunov/ans-utilities/src/master/hermite/

                    Knock yourself out, if you so desire. I'm certainly not investing the time to see if -g makes a significant difference, which I strongly suspect it doesn't.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    1
                    • Kent-DorfmanK Kent-Dorfman

                      @kshegunov I wrote absolutely nothing about "-g". I still maintain that simple -O0 vs -O2 is NOT going to divide performance by a factor of 10. I cannot begin to imagine how badly a person would have to design their algorithm to validate that level of performance hit. something other than compiler optimization is causing his hit...

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #22

                      @Kent-Dorfman said in Speed Optimization of C++ console application:

                      @kshegunov I wrote absolutely nothing about "-g". I still maintain that simple -O0 vs -O2 is NOT going to divide performance by a factor of 10. I cannot begin to imagine how badly a person would have to design their algorithm to validate that level of performance hit. something other than compiler optimization is causing his hit...

                      That strongly depends on the algorightm, I'd say.

                      Just imagine, a non optimized build that does not fit in the cache, so the CPU has to re-load stuff from memory all the time vs. the optimized build that runs fluently.

                      Factor 10 is probably not the normal case where you have to wait for I/O anyway, but for heavy computing it is easily possible.

                      Regards

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      0
                      • fcarneyF Offline
                        fcarneyF Offline
                        fcarney
                        wrote on last edited by
                        #23

                        This is starting to sound like a coding challenge. Can you write an algorithm that is slow the compiler can optimize and make fast? Like turning lead to gold.

                        C++ is a perfectly valid school of magic.

                        kshegunovK 1 Reply Last reply
                        2
                        • fcarneyF fcarney

                          This is starting to sound like a coding challenge. Can you write an algorithm that is slow the compiler can optimize and make fast? Like turning lead to gold.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #24

                          @fcarney said in Speed Optimization of C++ console application:

                          Can you write an algorithm that is slow the compiler can optimize and make fast? Like turning lead to gold.

                          As I wrote, any template nonsense you have (the deeper and nastier the better) fits into this category.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • Kent-DorfmanK Online
                            Kent-DorfmanK Online
                            Kent-Dorfman
                            wrote on last edited by
                            #25

                            Eh, the OP kind of disappeared so I guess it isn't that important to him. I'm more interested in knowing whether the windoze version in this exercise was compiled to CLR bytecode, which he never answered, and which IMHO invalidates any real comparison.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved