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 among following statements c/c++ code is faster?

Which among following statements c/c++ code is faster?

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
11 Posts 9 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.
  • S Offline
    S Offline
    shaktikanta
    wrote on last edited by
    #1

    int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.

    J M R A P 5 Replies Last reply
    0
    • S shaktikanta

      int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      It depends on the compiler and the optimization options. But in this simple case the generated code should be identical. You can check this by specifying a compiler option to generate assembly code (e.g. /Fas with Microsoft compilers) and compare the generated code.

      H 1 Reply Last reply
      0
      • S shaktikanta

        int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        You don't need to care; write readable code, the compiler will be more than sufficient to optimize it. They will be compile to the same thing.

        I'd rather be phishing!

        S 1 Reply Last reply
        0
        • S shaktikanta

          int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.

          R Offline
          R Offline
          Rage
          wrote on last edited by
          #4

          The second one is faster to type. :rolleyes:

          ~RaGE();

          I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Entropy isn't what it used to.

          1 Reply Last reply
          0
          • M Maximilien

            You don't need to care; write readable code, the compiler will be more than sufficient to optimize it. They will be compile to the same thing.

            I'd rather be phishing!

            S Offline
            S Offline
            shaktikanta
            wrote on last edited by
            #5

            I think the first one is faster. because, the in second - variable first ! converted - the newly converted variable is converted to 1/0 for if statement but in first- -the == operator check bit wise datas of 2 numbers, if matched 1/if not 0. so i think the first one is faster. Whats your view..

            M J 2 Replies Last reply
            0
            • S shaktikanta

              I think the first one is faster. because, the in second - variable first ! converted - the newly converted variable is converted to 1/0 for if statement but in first- -the == operator check bit wise datas of 2 numbers, if matched 1/if not 0. so i think the first one is faster. Whats your view..

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              THERE IS NO DIFFERENCE. For such simple code, the compiler will generate the same code. compile and generate the assembly code for both versions, and you will see that the code is the same. (testes with debug version on VS2012).

              I'd rather be phishing!

              1 Reply Last reply
              0
              • S shaktikanta

                int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                With something this trivial, it doesn't matter. If you want to write fast code, there are a ton of other bottlenecks that are more important than this.

                1 Reply Last reply
                0
                • S shaktikanta

                  I think the first one is faster. because, the in second - variable first ! converted - the newly converted variable is converted to 1/0 for if statement but in first- -the == operator check bit wise datas of 2 numbers, if matched 1/if not 0. so i think the first one is faster. Whats your view..

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

                  shaktikanta wrote:

                  I think the first one is faster.

                  The compiler need do nothing more than test for zero which the target CPU almost certainly has a single opcode for.

                  1 Reply Last reply
                  0
                  • S shaktikanta

                    int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.

                    P Offline
                    P Offline
                    ptse
                    wrote on last edited by
                    #9

                    Statement 2 should be faster. - no need to do parsing and comparison, just go to memory to negate, and see if it's above 0 or not. - compiler can generate machine code that would take less clock circles to execute the if() statement The speed differences between the 2 statements are negligible, - but statement 1 reads better, easier to understand and for better documentation.

                    L 1 Reply Last reply
                    0
                    • P ptse

                      Statement 2 should be faster. - no need to do parsing and comparison, just go to memory to negate, and see if it's above 0 or not. - compiler can generate machine code that would take less clock circles to execute the if() statement The speed differences between the 2 statements are negligible, - but statement 1 reads better, easier to understand and for better documentation.

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

                      ptse wrote:

                      Statement 2 should be faster.

                      They will both be the same; each being a machine code instruction that tests for zero.

                      1 Reply Last reply
                      0
                      • J Jochen Arndt

                        It depends on the compiler and the optimization options. But in this simple case the generated code should be identical. You can check this by specifying a compiler option to generate assembly code (e.g. /Fas with Microsoft compilers) and compare the generated code.

                        H Offline
                        H Offline
                        Heng Xiangzhong
                        wrote on last edited by
                        #11

                        I agree with you! In general, two cases are identical!

                        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