Announcement

Collapse
No announcement yet.

MIN, MAX are slooow..

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

  • MIN, MAX are slooow..

    Optimizing tip: I have used MIN&, MAX& quite a lot in my code without
    reflecting over possible slowness. Just did a test and found it's best
    to skip those commands all together. Following gives same result, but
    IF/THEN is much faster:
    Code:
      LOCAL I AS LONG, J AS LONG, t AS SINGLE
     
      t = TIMER
      FOR I = 1 TO 100000000
         IF I > 50000000 THEN
            J = I
         ELSE
            J = 50000000
         END IF
      NEXT
      MSGBOX STR$(TIMER - t) '<about 3 seconds in slow 133 MHz machine
     
      t = TIMER
      FOR I = 1 TO 100000000
          J = MAX&(50000000, I)
      NEXT
      MSGBOX STR$(TIMER - t) 'about 6 seconds in same machine
    ------------------
    Corrected stupid error after tips from Florent, see below..


    [This message has been edited by Borje Hagsten (edited April 28, 2001).]

  • #2
    Borje

    you must be tired the IF/THEN comparison is done
    with 10,000,000 iterations whereas the MAX timing
    code goes through 100,000,000 iterations. This is the cause
    for the huge difference.

    On my PII 400MHz IF/THEN clocks at 0.129~ seconds against
    0.180~ seconds. IF/THEN is faster but not by an order of
    magnitude...

    Cheers

    Florent





    [This message has been edited by Florent Heyworth (edited April 28, 2001).]

    Comment


    • #3
      Wops, yes - seems my mind already has gone to bed. Shall correct
      the sample above. Still, with proper numbers on slow 133 MHz machine,
      IF/THEN takes about 3 seconds, compared to 6 with MAX&. Not as much,
      but enough to rewrite some time critical code I have, where MIN/MAX
      have been used extensively..


      ------------------

      Comment


      • #4
        I just use PB/DOS but since i use MIN / MAX extensively too, Borje’ s post made me check this in PB/DOS, to notice that in PB/DOS too the IFs are faster. So thanks Borje for the idea.
        I’ m not posting any benchmark code because i simply adapted Borje’ s one and because i roughly benckmarked w/o kicking Win away (and because this isn’ t the PB/DOS forum). However the rough results have been 26” for IFs and 38” for MAX.

        Reason why i’ m posting here is, Borje are you going to put the IFs etc right in place of the MIN and MAX keywords, or are you going to write custom MIN / MAX functions and call them in place of the MIN and MAX keywords ?
        If you’ re going for functions, even though you probably already knew it, since you mentioned time critical code, i suggest you to test for the overhead of calling functions before making big work.
        Depending on how critical these calls are, you are subject to find that calling custom functions instead of PB’ s MIN / MAX eats some of your speed gains. After all, MIN / MAX are giving the programmer more than an IF structure does (1st, readability), so there is the risk that custom functions using IFs eat speed too.

        ------------------

        Comment


        • #5
          Nah, I have started replacing some of them with simple IF/THEN. In most cases,
          MIN&/MAX& has been use as replacement for "var = var2, IF var > max THEN", so
          two or three lines is enough.

          Note though, MIN&/MAX& still are pretty fast. One need to iterate millions of
          times to notice any difference so they are still very useful, except in real
          time-critical code.


          ------------------

          Comment


          • #6
            Code:
            J = 50000000               <<<< INSERT THIS
            FOR I = 1 TO 100000000
              IF I > 50000000 THEN
                    J = I
              ELSE                    <<< DELETE THESE TWO LINES
                    J = 50000000      <<<  
              END IF
            NEXT  MSGBOX STR$(TIMER - t) '<about 3 seconds in slow 133 MHz machine
            If you assign J the value 50000000 BEFORE entering the loop, the code will be even faster, as you no longer need to make any assignments within the loop unless and until I is greater than 50000000

            MCM

            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Ah yes - thanks Michael. My mind definitely was on leave yesterday..


              ------------------

              Comment


              • #8
                While substituting an IF..THEN for a simple MAX or MIN will often be ever-so-slightly faster, remember that MAX and MIN allow any number of arguments.

                How "simple" would the following code be when you substitute an IF-THEN block?

                Result? = MIN&(255, MAX&(0, Limit0&, Limit1&, Limit2%, Limit3#, [email protected],..., Limit99!))

                Horses for courses...

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  MAX and MIN may be slower that IF/THEN when you are comparing two numbers, but when you begin comparing three, four, etc. MAX and MIN will usually be faster. (I say "usually" because it depends on the statistical makeup of the values that you are testing.) I wrote a quick program to test 3 random numbers, and IF/THEN was about equal in speed to MAX. When I changed the test to 4 numbers MAX was the clear winner, and I would assume that that trend would continue.

                  And when I put my IF/THEN block into a function that I could call -- remember, MAX is a function so it's really not fair to compare it to a single in-line IF/THEN test -- the timing differences gave MAX an even bigger advantage. MAX was faster than IF/THEN in a 3-value test.

                  I also noticed that for every value I added to my test program, the IF/THEN block doubled in size, and it became very hard to read and debug. MAX and MIN also provide the advantage of "readability" and "maintainability".

                  The moral of the story is that you need to use the tool that is appropriate to the task. If you are comparing two values, IF X > Y THEN is clearly the most efficient way to do it.

                  -- Eric

                  ------------------
                  Perfect Sync Development Tools
                  Perfect Sync Web Site
                  Contact Us: mailto:[email protected][email protected]</A>



                  [This message has been edited by Eric Pearson (edited April 29, 2001).]
                  "Not my circus, not my monkeys."

                  Comment


                  • #10
                    To PB, don't underestimate the slowness of min/max.
                    I removed these also for time critical procedures.
                    Calling a procedure > 4000 times, is no fun.
                    A simple if and then was MUCH faster in my case.

                    They are very handy but if you need speed, wich most of the times depending on multiple pieces of code, min max is no good.



                    ------------------
                    hellobasic

                    Comment


                    • #11
                      Likely any language keyword statement supported by a compiler could be surpassed in
                      speed by a function performing a specific task which has less functionality
                      than the compiler's statement.

                      If speed is of the essence then a custom function may be warranted.
                      Otherwise, a 1000% increase in speed may be practically nothing when the 1000%
                      might be a function executing in 7us as opposed to the slow function executing in
                      70us.

                      Comment


                      • #12
                        "To PB, don't underestimate the slowness of min/max...min max is no good"

                        The one thing we'll never underestimate is the ability of some programmers to draw incorrect and inappropriate conclusions, drawing upon sparse, anecdotal evidence. While it's possible your observation has some merit in a single case, it's sad that sweeping derogatory statements may affect those most in need of factual information: the newer programmer, and those without extensive experience with PowerBASIC products.

                        Every program on Earth would be faster if written in hand-optimized assembler by an expert programmer. Does that mean we should outlaw compilers? I hope not.

                        I could easily write inline code to replace the MID$() function at a faster speed. Should we therefore remove MID$() from PowerBASIC? Of course not.

                        Everyone's interests, including your own, might be much better served if we gave a bit more thought to accuracy prior to blasting any product.


                        Regards,

                        Bob Zale
                        PowerBASIC Inc.

                        ------------------

                        Comment


                        • #13
                          If you must you could try something like this (LONG only, untested = use at own risk):

                          'replaces I = MAX(I, J):
                          ! mov eax, J
                          ! cmp eax, I
                          ! jle maxdone
                          ! mov I, eax
                          maxdone:


                          Peter.


                          ------------------
                          [email protected]
                          [email protected]

                          Comment


                          • #14
                            No one is blasting anything here, just sharing observations to help
                            each other write even faster code with your excellent product, Bob.

                            I agree that MIN/MAX is very good for many things, but sometimes it
                            can be good to test all built-in functions and compare, because even
                            if it may sound crazy to do a 100 million loop to see real difference,
                            every little thing builds up.

                            Among many other things, my main product is a translation engine for
                            languages. Huge and highly complicated code. By replacing all MIN/MAX
                            functions I used, I have actually managed to cut time by a second
                            second on a 100 page translation. You see, 100 pages may hold 1000
                            words per page, or more. That is at least 100,000 times the translation
                            engine is called upon and with +10 MIN/MAX functions in engine, that
                            means they are used at least a million times. Things easily build up
                            in text parsers.

                            Re-write the whole thing in asm? Well, have done a few inline snippets,
                            but to rewrite the whole thing - naah, life is too short..


                            ------------------

                            Comment


                            • #15
                              "No one is blasting anything here, just sharing..."

                              That's an interesting perspective -- very charitable, too. However, in my opinion, it's not accurate.

                              Thousands of folks visit this site daily. The vast majority are lurkers, never posting a message. Many are "first-time" visitors, with little, if any experience with PowerBASIC and our third-party group of vendors. When they see a public statement like "... min max is no good" published on our web site, I believe many may draw an incorrect conclusion, especially if it is left unchallenged. As I said before, I believe that sweeping, derogatory comments are misleading to many, and simply inappropriate unless they can be validated with hard, factual data. Frankly, I believe this type of generalization is just invalid for comparison of a general purpose language function to specific, hand-coded, inline programming.

                              Everyone is entitled to their opinion, and the right to distribute it within reasonable limits. Even here on our facilities. But some of these sweeping negative comments, even if unintended, are bound to influence those who may be a bit less informed on the item of interest. I simply mentioned that "Everyone's interests, including your own, might be much better served if we gave a bit more thought to accuracy prior to blasting any product in a public forum."

                              You're free to attach whatever value you choose to my request.

                              Regards,

                              Bob Zale
                              PowerBASIC Inc.


                              ------------------

                              Comment


                              • #16
                                I wonder what the big deal is about, most languages are able to do certain
                                tasks in a number of different ways, it is the programmers choice to select
                                the techniques that best suite the task at hand.

                                When I hear statements about this function is better than that function in my
                                application, my normal response is "then use it", don't pontificate about it.
                                There are many reasons why different overlapping capacities exist in a
                                programming language, if you are writing complex logic, clarity and
                                convenience are of great advantage where if you are chasing the last cycle
                                for a speed critical operation, minimum instruction path code become
                                appropriate.

                                The example I keep in mind is coding a large application's message processing
                                function in assembler, it can be done but you would have it ready for the
                                release of win3k. I will opt for multiply nested Select Case statements every
                                time as it produces clear and maintainable code where a collection of
                                compares, jumps and labels produces nightmares.

                                If someone wrote an algorithm that performed speed critical operations and
                                complained that IF / THEN was no good in speed terms, I would be inclined to
                                say that perhaps they have not selected the correct capacity for the
                                algorithm.

                                The responsibility for an algorithm design rests properly with the programmer
                                writing it, not with the tools he/she is using, if you use the incorrect
                                capacity in an algorithm, it just means that you have written sub-optimal
                                code.

                                Regards,

                                [email protected]


                                ------------------
                                hutch at movsd dot com
                                The MASM Forum

                                www.masm32.com

                                Comment


                                • #17
                                  The deal seems to be that we should not post observations so they
                                  can be examined by others. I don't understand the fuzz, but begin
                                  to understand why most people are lurkers..


                                  ------------------

                                  Comment


                                  • #18
                                    Hi Borje

                                    I didn't read Bob's post as saying we should not
                                    post our observations but rather that we should
                                    try to phrase our observations in a way that
                                    is less prone to being misunderstood by a newbie
                                    PB user.

                                    i.e instead of saying such and such is slow
                                    maybe state something like 'the following way
                                    is quicker in this and that circumstance'.

                                    Cheers

                                    Florent (cutting hairs in half )?



                                    ------------------

                                    Comment


                                    • #19
                                      Hi Borje,

                                      You've got to start reading between the lines...

                                      [humor on]

                                      What Bob is really saying...

                                      "PBDLL7 is about to go GOLD and we don't have
                                      time to change the MIN/MAX function now!"

                                      [humor off]

                                      Regards,
                                      --Bob

                                      ------------------
                                      "It was too lonely at the top".

                                      Comment


                                      • #20
                                        Sorry, attached wrong value to Bob's request. Maybe he didn't mean
                                        my original post, but later answer. Probably read too much between
                                        the lines..


                                        ------------------

                                        Comment

                                        Working...
                                        X