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. YASAQ Yet another silly academic question - how does "code optimization" really works?

YASAQ Yet another silly academic question - how does "code optimization" really works?

Scheduled Pinned Locked Moved C / C++ / MFC
questionalgorithmsperformancetutorial
13 Posts 6 Posters 2 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.
  • V Vaclav_

    Run into a coder who for whatever and unknown reason posted in this "style": .... return A+B; return A+B; } and duplicated few other lines of code also. Obviously the second "return" won't ever be executed ( unless used after "if" for example ). My question is - does the "optimizer" do anything with subsequent and identical lines of code assuming the first line does not modify anything? Just asking, and yes , I did ask the originator, but he was too busy "fixing" his illogical code elsewhere and did not seems interested in such trivial matters. Cheers Vaclav

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

    You might try it with your favorite compiler.

    In testa che avete, signor di Ceprano?

    1 Reply Last reply
    0
    • L Lost User

      Any half decent compiler would optimize out the second statement as it serves no purpose.

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

      True. Anyway any decent compiler would actively refuse to compile such a code.

      In testa che avete, signor di Ceprano?

      L 1 Reply Last reply
      0
      • CPalliniC CPallini

        True. Anyway any decent compiler would actively refuse to compile such a code.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #6

        VS2013 (actually the compiler) compiles it fine.

        CPalliniC 1 Reply Last reply
        0
        • L Lost User

          VS2013 (actually the compiler) compiles it fine.

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

          Doesn't find it outrageous? :laugh:

          In testa che avete, signor di Ceprano?

          M 1 Reply Last reply
          0
          • V Vaclav_

            Run into a coder who for whatever and unknown reason posted in this "style": .... return A+B; return A+B; } and duplicated few other lines of code also. Obviously the second "return" won't ever be executed ( unless used after "if" for example ). My question is - does the "optimizer" do anything with subsequent and identical lines of code assuming the first line does not modify anything? Just asking, and yes , I did ask the originator, but he was too busy "fixing" his illogical code elsewhere and did not seems interested in such trivial matters. Cheers Vaclav

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

            Vaclav_Sal wrote:

            does the "optimizer" do anything with subsequent and identical lines of code

            Why does it matter? Code itself requires very little space in pretty much all modern applications perhaps only excluding very small (device itself is small) embedded processing apps. And since the line can never execute, regardless of anything like a normal language, that means the space that the code actually takes up is the only part that is relevant in any normal 'optimization' discussion.

            V 1 Reply Last reply
            0
            • J jschell

              Vaclav_Sal wrote:

              does the "optimizer" do anything with subsequent and identical lines of code

              Why does it matter? Code itself requires very little space in pretty much all modern applications perhaps only excluding very small (device itself is small) embedded processing apps. And since the line can never execute, regardless of anything like a normal language, that means the space that the code actually takes up is the only part that is relevant in any normal 'optimization' discussion.

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #9

              "Why does it matter?" Why do you bother to reply if you cannot stay on subject and really answer the question? To everybody else: Observed on C++ code written for Arduino "toy" controller. "They" use some version of cpp compiler, sorry do not know exactly which one. It changes ( version ) often. I agree that such code should not compile on limited resources "embedded" hardware, as pointed out. But the compiler does not know that, right? There may be an option in compiler setup,but I have not search for it. Maybe when I get my messy code fixed I'll check it. Thanks Cheers Vaclav

              L J M 3 Replies Last reply
              0
              • V Vaclav_

                "Why does it matter?" Why do you bother to reply if you cannot stay on subject and really answer the question? To everybody else: Observed on C++ code written for Arduino "toy" controller. "They" use some version of cpp compiler, sorry do not know exactly which one. It changes ( version ) often. I agree that such code should not compile on limited resources "embedded" hardware, as pointed out. But the compiler does not know that, right? There may be an option in compiler setup,but I have not search for it. Maybe when I get my messy code fixed I'll check it. Thanks Cheers Vaclav

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                Vaclav_Sal wrote:

                I agree that such code should not compile on limited resources "embedded" hardware, as pointed out. But the compiler does not know that, right?

                Well it should; 'general' compilers have an option to tell them the target hardware. Without such an option how can they know what code to generate? I am still not sure what this question is really about, do you have an actual problem that needs resolving?

                1 Reply Last reply
                0
                • V Vaclav_

                  "Why does it matter?" Why do you bother to reply if you cannot stay on subject and really answer the question? To everybody else: Observed on C++ code written for Arduino "toy" controller. "They" use some version of cpp compiler, sorry do not know exactly which one. It changes ( version ) often. I agree that such code should not compile on limited resources "embedded" hardware, as pointed out. But the compiler does not know that, right? There may be an option in compiler setup,but I have not search for it. Maybe when I get my messy code fixed I'll check it. Thanks Cheers Vaclav

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #11

                  Vaclav_Sal wrote:

                  "Why does it matter?"
                  Why do you bother to reply if you cannot stay on subject and really answer the question

                  You said "optimization". Focusing on 'micro' optimization is meaningless. Focusing on business optimization is very relevant and thus the context of my response. So yes it is on subject in less you meant something else when you said "optimization".

                  Vaclav_Sal wrote:

                  But the compiler does not know that, right?

                  When one is targeting specific 'small' embedded spaces the following is probably always relevant (I only say probably because I am not familiar with every possible small device.) - The entire application must take into account the embedded space. - I doubt seriously that the cross compiler would not be specific to the device and perhaps even specific to the version of the device (certainly true is all of the limited cases I have worked with.) - Cross compilers can often have language limitations. For example (quite a bit in the past) a C cross compiler that I used did not support floating point numbers in any form.

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Doesn't find it outrageous? :laugh:

                    M Offline
                    M Offline
                    Mike Nordell
                    wrote on last edited by
                    #12

                    VC6-VC10 produce unreachable code warnings (C4702) /W4, but much to my surprise not at /W3.

                    1 Reply Last reply
                    0
                    • V Vaclav_

                      "Why does it matter?" Why do you bother to reply if you cannot stay on subject and really answer the question? To everybody else: Observed on C++ code written for Arduino "toy" controller. "They" use some version of cpp compiler, sorry do not know exactly which one. It changes ( version ) often. I agree that such code should not compile on limited resources "embedded" hardware, as pointed out. But the compiler does not know that, right? There may be an option in compiler setup,but I have not search for it. Maybe when I get my messy code fixed I'll check it. Thanks Cheers Vaclav

                      M Offline
                      M Offline
                      Mike Nordell
                      wrote on last edited by
                      #13

                      "They" use some version of cpp compiler, sorry do not know exactly which one. Seems likely it'd be a GCC, or nowadays maybe even LLVM. Have a look at command line options -W4 or -Wall. One of the two options (or very similar) would probably tell you "Silly user, now you gone and made a mess out of stuff again!". :-)

                      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