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. Other Discussions
  3. The Weird and The Wonderful
  4. Performance Genius

Performance Genius

Scheduled Pinned Locked Moved The Weird and The Wonderful
dockerperformancequestionannouncement
21 Posts 14 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.
  • I Ibrahim Yusuf

    Really a way to show performance improvement. :doh: Version 1.0

    for ( int i = 0; i < data.Count; i++ )
    {
    Thread.Sleep(200);
    container.Add(data[i].Items);
    }

    Version 2.0

    for ( int i = 0; i < data.Count; i++ )
    {
    Thread.Sleep(100);
    container.Add(data[i].Items);
    }

    Version 3.0

    for ( int i = 0; i < data.Count; i++ )
    {
    container.Add(data[i].Items);
    }

    Should I tell the boss? X|

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #8

    Isn't there a version 2.5? Thread.Sleep(0); ;P

    1 Reply Last reply
    0
    • I Ibrahim Yusuf

      Really a way to show performance improvement. :doh: Version 1.0

      for ( int i = 0; i < data.Count; i++ )
      {
      Thread.Sleep(200);
      container.Add(data[i].Items);
      }

      Version 2.0

      for ( int i = 0; i < data.Count; i++ )
      {
      Thread.Sleep(100);
      container.Add(data[i].Items);
      }

      Version 3.0

      for ( int i = 0; i < data.Count; i++ )
      {
      container.Add(data[i].Items);
      }

      Should I tell the boss? X|

      R Offline
      R Offline
      Ravi Sant
      wrote on last edited by
      #9

      another version may come up with something like for ( int i = 0; i < data.Count; container.Add(data[i].Items,i++ )

      Y 1 Reply Last reply
      0
      • S Steve Caine

        Definitely. There's no excuse for for ( int i = 0; i < data.Count; i++ ) instead of the obvious, clearly superior and faster for ( int i = 0; i < data.Count; ++i ) ;P

        B Offline
        B Offline
        BillW33
        wrote on last edited by
        #10

        Yes, it is surprising how few C++ programmers know that ++i executes faster than i++ :)

        Just because the code works, it doesn't mean that it is good code.

        G 1 Reply Last reply
        0
        • S Steve Caine

          Definitely. There's no excuse for for ( int i = 0; i < data.Count; i++ ) instead of the obvious, clearly superior and faster for ( int i = 0; i < data.Count; ++i ) ;P

          E Offline
          E Offline
          ely_bob
          wrote on last edited by
          #11

          The really sad thing is that most programmers don't know that i-- is the fastest way of the four... :-D

          I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
          -----
          "The conversations he was having with himself were becoming ominous."-.. On the radio...

          1 Reply Last reply
          0
          • B BillW33

            Yes, it is surprising how few C++ programmers know that ++i executes faster than i++ :)

            Just because the code works, it doesn't mean that it is good code.

            G Offline
            G Offline
            ghle
            wrote on last edited by
            #12

            CIDev wrote:

            Yes, it is surprising how few C++ programmers know that ++i executes faster than i++ Smile

            Look at the assembly and you'll see why. Fewer instructions to execute.:thumbsup:

            Gary

            S 1 Reply Last reply
            0
            • G ghle

              CIDev wrote:

              Yes, it is surprising how few C++ programmers know that ++i executes faster than i++ Smile

              Look at the assembly and you'll see why. Fewer instructions to execute.:thumbsup:

              Gary

              S Offline
              S Offline
              Steve Caine
              wrote on last edited by
              #13

              Because ++i doesn't have to keep track of/return the previous value of i.

              1 Reply Last reply
              0
              • S Steve Caine

                Definitely. There's no excuse for for ( int i = 0; i < data.Count; i++ ) instead of the obvious, clearly superior and faster for ( int i = 0; i < data.Count; ++i ) ;P

                R Offline
                R Offline
                realJSOP
                wrote on last edited by
                #14

                Is there some subtle sarcasm going on here? I've never seen "++i" used (in a for loop) - ever. EDIT ========== Just to see if you guys are chain yankin' here, I tried it both ways : 1 million iterations = both completed the iteration in 0.0040002 seconds 10 million iterations = i++ took 0.0350020, and ++i took 0.0340019 seconds

                ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                -----
                You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                -----
                "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

                modified on Sunday, January 2, 2011 9:42 AM

                F S 2 Replies Last reply
                0
                • R realJSOP

                  Is there some subtle sarcasm going on here? I've never seen "++i" used (in a for loop) - ever. EDIT ========== Just to see if you guys are chain yankin' here, I tried it both ways : 1 million iterations = both completed the iteration in 0.0040002 seconds 10 million iterations = i++ took 0.0350020, and ++i took 0.0340019 seconds

                  ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                  -----
                  You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                  -----
                  "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

                  modified on Sunday, January 2, 2011 9:42 AM

                  F Offline
                  F Offline
                  fjdiewornncalwe
                  wrote on last edited by
                  #15

                  Does this mean the wife is not up yet as is the case for me and that it is best to let her sleep in on Sunday morning? (Might be a good idea considering she's quick on the draw)... :)

                  I wasn't, now I am, then I won't be anymore.

                  1 Reply Last reply
                  0
                  • R realJSOP

                    Is there some subtle sarcasm going on here? I've never seen "++i" used (in a for loop) - ever. EDIT ========== Just to see if you guys are chain yankin' here, I tried it both ways : 1 million iterations = both completed the iteration in 0.0040002 seconds 10 million iterations = i++ took 0.0350020, and ++i took 0.0340019 seconds

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    "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

                    modified on Sunday, January 2, 2011 9:42 AM

                    S Offline
                    S Offline
                    Steve Caine
                    wrote on last edited by
                    #16

                    John Simmons / outlaw programmer wrote: "Is there some subtle sarcasm going on here? I've never seen "++i" used (in a for loop) - ever." Somewhat subtle. The increment operator is slightly more efficient in its prefix version (++i) because it just increments 'i' and returns its value. The postfix version has to do more work because it returns the *previous* version of 'i', so it has to store that value somewhere before incrementing i. If you're just incrementing 'i', it makes more sense to use the more efficient prefix version. It's just better form, and encourages good habits. (For example, applying it to something more complex than an integer, say an iterator to an STL container class, might produce a more significant performance hit than the miniscule one your test found.) Yet you're right, most of the instances of '++' or' --' I've seen in code, particularly in 'for' loops, is the postfix version. There's no reason for it, yet somehow that has become a near-universal meme in programming. Perhaps because the most common example beginning programmers encounter is the classic C-string copy code snippet, where you really *do* want to use the postfix version:   while (*dst++ = *src++)     ; So I was making a joke that the real coding outrage in the original poster's message was using 'i++' instead of '++i', as if I had completely missed the real outrage of sleeping the thread in each pass through the 'for' loop. (Getting off my soapbox now.)

                    1 Reply Last reply
                    0
                    • R Ravi Sant

                      another version may come up with something like for ( int i = 0; i < data.Count; container.Add(data[i].Items,i++ )

                      Y Offline
                      Y Offline
                      yxhu
                      wrote on last edited by
                      #17

                      improved version of above can be like: for ( int i = 0; i < data.Count; container.Add(data[i++].Items)

                      A 1 Reply Last reply
                      0
                      • Y yxhu

                        improved version of above can be like: for ( int i = 0; i < data.Count; container.Add(data[i++].Items)

                        A Offline
                        A Offline
                        Adrian0
                        wrote on last edited by
                        #18

                        I can improve even that code for ( int i = 0; i < data.Count; container.Add(data[++i].Items)

                        Y 1 Reply Last reply
                        0
                        • A Adrian0

                          I can improve even that code for ( int i = 0; i < data.Count; container.Add(data[++i].Items)

                          Y Offline
                          Y Offline
                          yxhu
                          wrote on last edited by
                          #19

                          Adrian0, unfortunately, your solution was invalid. As ++i will be calculated(increased) before accessing the element of data collection. container will never have data[0], plus you will receive an out of index exception at the final loop of this for expression. Say data collection has 4 elements. Each iteration looks like: Loop 1, i initialised = 0: container.Add(data[1]).Items Loop 2, i initialised = 1: container.Add(data[2]).Items Loop 3, i initialised = 2: container.Add(data[3]).Items Loop 3, i initialised = 3: container.Add(data[4]).Items <<< Exception here When i++ or ++i is within an expression, ++i has the highest priority to get executed bofere the reset of the expression. In contrast, i++ is get executed after the whole expression has been executed. container.Add(data[i++].Items) //Get the ith element of data collection, append to container and then increase i by 1 container.Add(data[++i].Items) //Increase i by 1, then get the 1th element of data collection to append to the container.

                          A 1 Reply Last reply
                          0
                          • Y yxhu

                            Adrian0, unfortunately, your solution was invalid. As ++i will be calculated(increased) before accessing the element of data collection. container will never have data[0], plus you will receive an out of index exception at the final loop of this for expression. Say data collection has 4 elements. Each iteration looks like: Loop 1, i initialised = 0: container.Add(data[1]).Items Loop 2, i initialised = 1: container.Add(data[2]).Items Loop 3, i initialised = 2: container.Add(data[3]).Items Loop 3, i initialised = 3: container.Add(data[4]).Items <<< Exception here When i++ or ++i is within an expression, ++i has the highest priority to get executed bofere the reset of the expression. In contrast, i++ is get executed after the whole expression has been executed. container.Add(data[i++].Items) //Get the ith element of data collection, append to container and then increase i by 1 container.Add(data[++i].Items) //Increase i by 1, then get the 1th element of data collection to append to the container.

                            A Offline
                            A Offline
                            Adrian0
                            wrote on last edited by
                            #20

                            Good work yxhu. Didn't recognize that. But if you are that fussy watch your parenthesis

                            Loop 3, i initialised = 3:
                            container.Add(data[4]).Items <<< Exception here

                            Your code wouldn't compile because .Add() is usually of type void. ;) greets Adrian

                            Y 1 Reply Last reply
                            0
                            • A Adrian0

                              Good work yxhu. Didn't recognize that. But if you are that fussy watch your parenthesis

                              Loop 3, i initialised = 3:
                              container.Add(data[4]).Items <<< Exception here

                              Your code wouldn't compile because .Add() is usually of type void. ;) greets Adrian

                              Y Offline
                              Y Offline
                              yxhu
                              wrote on last edited by
                              #21

                              Lol... that was a typo. Thanks Adrian0. :thumbsup:

                              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