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#
  4. Which one is faster ?

Which one is faster ?

Scheduled Pinned Locked Moved C#
question
10 Posts 4 Posters 1 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.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hello. Which one is faster ? this one :

    int x , y , z ;
    .
    .
    .
    //some codes 1 == some codes 2
    if (x > y) { /*some codes 1*/ }
    else if ( z > x ) { /*some codes 2*/ }

    or this one :

    int x , y , z ;
    .
    .
    .
    if (x > y || z > x ) { /*some codes1*/ }

    Thanks.

    realJSOPR M 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hello. Which one is faster ? this one :

      int x , y , z ;
      .
      .
      .
      //some codes 1 == some codes 2
      if (x > y) { /*some codes 1*/ }
      else if ( z > x ) { /*some codes 2*/ }

      or this one :

      int x , y , z ;
      .
      .
      .
      if (x > y || z > x ) { /*some codes1*/ }

      Thanks.

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      I really don't understand why you're asking this here. Why don't you set up a test that performs the comparisons a few thousand times and see for yourself?

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      M 1 Reply Last reply
      0
      • realJSOPR realJSOP

        I really don't understand why you're asking this here. Why don't you set up a test that performs the comparisons a few thousand times and see for yourself?

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        Thank you John .

        John Simmons / outlaw programmer wrote:

        Why don't you set up a test that performs the comparisons a few thousand times and see for yourself?

        Well, I think it doesn't show the real result, because it will depend to our system configuration. Isn't it ?

        realJSOPR K 2 Replies Last reply
        0
        • M Mohammad Dayyan

          Thank you John .

          John Simmons / outlaw programmer wrote:

          Why don't you set up a test that performs the comparisons a few thousand times and see for yourself?

          Well, I think it doesn't show the real result, because it will depend to our system configuration. Isn't it ?

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          Yes, how fast it is depends on the system it's being run on, but since you're simply trying to determine which one is faster, the results should be comparative since you'd be running the test on the same system. You may have to run several hundred thousand iterations to get a measurable time.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          M 1 Reply Last reply
          0
          • realJSOPR realJSOP

            Yes, how fast it is depends on the system it's being run on, but since you're simply trying to determine which one is faster, the results should be comparative since you'd be running the test on the same system. You may have to run several hundred thousand iterations to get a measurable time.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            M Offline
            M Offline
            Mohammad Dayyan
            wrote on last edited by
            #5

            Thank you my friend ;)

            1 Reply Last reply
            0
            • M Mohammad Dayyan

              Hello. Which one is faster ? this one :

              int x , y , z ;
              .
              .
              .
              //some codes 1 == some codes 2
              if (x > y) { /*some codes 1*/ }
              else if ( z > x ) { /*some codes 2*/ }

              or this one :

              int x , y , z ;
              .
              .
              .
              if (x > y || z > x ) { /*some codes1*/ }

              Thanks.

              M Offline
              M Offline
              Mark Churchill
              wrote on last edited by
              #6

              It doesnt matter. Most likely both of those examples will JIT down to identical code. If they don't now, then they might later. Performance difference of those examples comes down to the platform implementation, which is really none of your business in a high level language like C#. You don't need to worry about performance at that level. If your application is running slowly, I can guarantee its not due to trivial things like this.

              Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
              Entanglar: .Net game engine featuring automatic networking and powerful HLSL gfx binding.

              M 1 Reply Last reply
              0
              • M Mohammad Dayyan

                Thank you John .

                John Simmons / outlaw programmer wrote:

                Why don't you set up a test that performs the comparisons a few thousand times and see for yourself?

                Well, I think it doesn't show the real result, because it will depend to our system configuration. Isn't it ?

                K Offline
                K Offline
                King Julien
                wrote on last edited by
                #7

                not actually..... because you are going to execute and check both the code in the same machine.... right??? so system's performance will not have any impact on this....

                Have a Happy Coding.....

                M 1 Reply Last reply
                0
                • K King Julien

                  not actually..... because you are going to execute and check both the code in the same machine.... right??? so system's performance will not have any impact on this....

                  Have a Happy Coding.....

                  M Offline
                  M Offline
                  Mohammad Dayyan
                  wrote on last edited by
                  #8

                  King Julien wrote:

                  because you are going to execute and check both the code in the same machine.... right???

                  No, it will run in another systems with different hardware !

                  1 Reply Last reply
                  0
                  • M Mark Churchill

                    It doesnt matter. Most likely both of those examples will JIT down to identical code. If they don't now, then they might later. Performance difference of those examples comes down to the platform implementation, which is really none of your business in a high level language like C#. You don't need to worry about performance at that level. If your application is running slowly, I can guarantee its not due to trivial things like this.

                    Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                    Entanglar: .Net game engine featuring automatic networking and powerful HLSL gfx binding.

                    M Offline
                    M Offline
                    Mohammad Dayyan
                    wrote on last edited by
                    #9

                    Mark Churchill wrote:

                    You don't need to worry about performance at that level. If your application is running slowly, I can guarantee its not due to trivial things like this.

                    Actually , I'm working on Image Processing. So I think , the simple things like these are important for reaching the most speed !

                    M 1 Reply Last reply
                    0
                    • M Mohammad Dayyan

                      Mark Churchill wrote:

                      You don't need to worry about performance at that level. If your application is running slowly, I can guarantee its not due to trivial things like this.

                      Actually , I'm working on Image Processing. So I think , the simple things like these are important for reaching the most speed !

                      M Offline
                      M Offline
                      Mark Churchill
                      wrote on last edited by
                      #10

                      If you are really concerned you should take a look at the MSIL output of both approaches. This behaviour is not specified by the compiler - I'd suggest looking for algorithmetic speedups over "cargo-cult" coding kind of tricks. Either that or switch to an unmanaged compiler for absolute maximum peformance. Take a look at the Mono.SIMD library as well.

                      Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                      Entanglar: .Net game engine featuring automatic networking and powerful HLSL gfx binding.

                      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