ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

DOW x <= y is 3 times faster than using for x = number downto 1

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Re: DOW x <= y is 3 times faster than using for x = number downto 1

    Sven and Vikramx... great work. Its really something that this has intrigued people so much

    About the integer bit. I learned that trick from someone a couple of years ago. Someone had mentioned that you should use integers for looping variables because they were incredibly faster.... so I did and have been using 10i 0 ever since.

    I think that I also remember hearing that a dow is basically just GOTO under the covers so this would explain why the times are very similar.

    I also test many times and with many different iteration sizes and the ratios are very consistent. Do/For is about 1/3. I just saw some code yesterday that has a for nested in a for.... I wonder how nested Do's compare to nested For's..... time to test
    Your future President
    Bryce

    ---------------------------------------------
    http://www.bravobryce.com

    Comment


    • #17
      Re: DOW x <= y is 3 times faster than using for x = number downto 1

      So interestingly the ratios stayed about the same. The nested DOW was 3 times faster than the nested FOR.
      Your future President
      Bryce

      ---------------------------------------------
      http://www.bravobryce.com

      Comment


      • #18
        Re: DOW x <= y is 3 times faster than using for x = number downto 1

        Hi All,
        what I found most remarkable is the fact that the optimizer of the compiler can significantly reduce the runtime of the do loops and reduced the runtime of the for loops only around 5% ... So if you use do loops instead of for loops in environments with huge amounts of simple iterations and run the optimizer while compiling you get 12 to 13 times faster routines.
        Sven

        The best way to prove your knowledge is to share it ...

        Comment


        • #19
          Re: DOW x <= y is 3 times faster than using for x = number downto 1

          Guys, you need to step back and at take a look at what the DOW and FOR routines are actually doing. This shouldn't be any surprise as to why the DOW is faster. Your doing a lot more work behind the scenes with the FOR statement. With the DOW your only checking if a=b and incrementing a counter. Now think about what has to be done when your doing the FOR.

          Comment


          • #20
            Re: DOW x <= y is 3 times faster than using for x = number downto 1

            What's the big difference DAG? With the for loop behind the scenes I would expect that after each iteration it increments an counter and checks condition.... just like a DOW... except the compiler takes care of that mechanism for you.... I would actually think that there might be a way to optimize it so that its faster than the DOW... Not sure what you are getting at? What do you know about a for loop behind the scenes that I'm missing?
            Your future President
            Bryce

            ---------------------------------------------
            http://www.bravobryce.com

            Comment


            • #21
              Re: DOW x <= y is 3 times faster than using for x = number downto 1

              When you run the command which iteration are you on the first or subsequent ones. If it the first it needs to clear the counter. What are you incrementing by, Are you doing a DOWNTO. What are all the parameters on the command and what do i do with them It most likely runs faster with the BY because your telling it how much to increment, whereas without it it needs to determine that first. Again, look at what your really doing on the DOW compared to what the FOR statement has to actually go through.

              Comment


              • #22
                Re: DOW x <= y is 3 times faster than using for x = number downto 1

                But a lot of those decisions don't need to be made each iteration, they would be seen by the compiler and the machine code would be structured to handle that. I would think that the for loop machine code would be quite similar to the dow code... not exact I'm sure, but they should be quite close I would think.

                Is it the first run? set a flag that is part of the decision logic.... run time check. --- does that 1 extra logic check actually make the difference??? If FirstRun and a condition b; as opposed to just If a condition b; ??? Maybe... but I doubt it...

                Doing a DOWNTO? that just means that the counter decrements... compile time check.
                What am I inc/dec by? If I don't specify the default is 1. compile time check.

                Still not seeing why the difference is 3x... and worse when using the optimizer.
                Your future President
                Bryce

                ---------------------------------------------
                http://www.bravobryce.com

                Comment


                • #23
                  Re: DOW x <= y is 3 times faster than using for x = number downto 1

                  It may not need to do those decisions each time but it needs to check each time to determine if it needs to be done. More operations means its going to run slower. AS I stated before on the DOW your only checking if A=B and incrementing A (2 operations). Your doing a lot more than that with the FOR statement.

                  Comment


                  • #24
                    Re: DOW x <= y is 3 times faster than using for x = number downto 1

                    The differences between the the dow / dou opcodes and the for opcode is that the dow / dou are basically simple compare and branch routines.

                    The FOR opcode does quite a bit more under the covers. (It performs the same type of compare and branch, but also has to handle incrementing / decrementing a value based upon another increment / decrement variable.) It also has some initial setup work to do.

                    When you compare the DOW and FOR opcodes performing the same type of loop, you would expect somewhere near the same processing time. It could be simply a matter of IBM not having optimized the FOR opcode to the level in the compiler they could, simply because of time & expense to do so doesnt outweigh the benefit received. When you are only saving about 2 seconds of CPU time per 100million iterations (like it is on my i515), it may not be worth the effort to optimize the opcode further in the compiler.
                    Michael Catalani
                    IS Director, eCommerce & Web Development
                    Acceptance Insurance Corporation
                    www.AcceptanceInsurance.com
                    www.ProvatoSys.com

                    Comment


                    • #25
                      Re: DOW x <= y is 3 times faster than using for x = number downto 1

                      Hi

                      Just tested
                      Code:
                      c     1             do        c             i 
                      c                   enddo     1
                      It performs around 15% faster than... DOW
                      They both perform the same with OPTIMIZE(*FULL)
                      Nicolas

                      Comment


                      • #26
                        Re: DOW x <= y is 3 times faster than using for x = number downto 1

                        Well I did some looking around and found that the FOR wasn't introduced until V4R4, so its not like it was an original construct. My guess would be that it was added later in the game as opposed to being an original language construct. So the branching logic and such is probably not shared with the DOW. I agree that the FOR has a bit more to do at the beginning, but if that is what was holding it up then you would see the gap be worse at a small number of iterations and the times would get closer and closer to the same relative time to run. That doesn't happen....its flat out 3 times slower than DOW, for any number of iterations... from 100,000 to 100,000,000... same time ratios.

                        I agree that at this point in time they probably don't think it would be worth the time to optimize the compiler, but when I look at other compilers for languages like C and they don't have these issues I am slightly disappointed. It could be better, and it should be better... I know 2 seconds for 100 milltion iterations doesn't sound like much..... but how many for iterations does your system process each day? Less than 100million? more? I know, seconds don't matter at this time in computing history... guess I should probably just let it go
                        Your future President
                        Bryce

                        ---------------------------------------------
                        http://www.bravobryce.com

                        Comment


                        • #27
                          Re: DOW x <= y is 3 times faster than using for x = number downto 1

                          If it makes you feel any better, the for loop in C is slightly slower than the while loop.
                          Michael Catalani
                          IS Director, eCommerce & Web Development
                          Acceptance Insurance Corporation
                          www.AcceptanceInsurance.com
                          www.ProvatoSys.com

                          Comment


                          • #28
                            Re: DOW x <= y is 3 times faster than using for x = number downto 1

                            I know that this:
                            Code:
                                 C           MMDDYY    MULT 10000.01  YYMMDD

                            Is muuuuuuuch slower than this:
                            Code:
                                 C                     MOVELMMDDYY    MMDD
                                 C                     MOVE MMDDYY    YY
                                 C                     MOVELYY        YYMMDD
                                 C                     MOVE MMDD      YYMMDD

                            Somewhere along the line I also heard that if you're trying to test for even quarter hour increments, it's better to MULT by 0.25 than DIV by 4.

                            And finally, if you want to delete all children files of a parent file, matching records is extremely fast.
                            Last edited by Chipper; June 22, 2011, 10:41 AM. Reason: Fix typo (6/21); change your to you're (6:22)
                            http://www.linkedin.com/in/chippermiller

                            Comment


                            • #29
                              Re: DOW x <= y is 3 times faster than using for x = number downto 1

                              Also:

                              Never multiple by -1, always subtract from 0.
                              "Time passes, but sometimes it beats the <crap> out of you as it goes."

                              Comment


                              • #30
                                Re: DOW x <= y is 3 times faster than using for x = number downto 1

                                Never multiply by by 2, 4, 8, 16, etc. Shift the register left 1, 2, 3, 4, etc bytes.
                                Michael Catalani
                                IS Director, eCommerce & Web Development
                                Acceptance Insurance Corporation
                                www.AcceptanceInsurance.com
                                www.ProvatoSys.com

                                Comment

                                Working...
                                X