my code is too slow, how do I speed it up?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence k

    my code is too slow, how do I speed it up?


    The following function is way too slow. If anyone has any suggestions
    about how to speed it up, I'd be grateful for them. We have to call
    this function 36 times on one page, and I think each time it takes half
    a second, so it adds up to maybe 18 seconds, which is a lot when you're
    showing software to a client. The reponse we get is "Why is it so
    slow?"


    <?php


    function showBandAlpha($ firstFewCharsWe WantToMatch=fal se,
    $fieldToGet="cb Headline") {
    // 05-22-06 - I'm working with Hoyt on www.monkeyclaus.org, he wants
    to list all the
    // music bands, segmented by the first letter, so we need to all the
    bandnames as an array,
    // and then checked against the first letter. That is, he wants to
    be able to show all
    // the music bands whose name starts with the letter "A" by putting
    this line in his design:
    //
    // showBandAlpha(" a");
    //
    // and all the bands whose name starts with the letter "b" should
    appear wherever Darren puts the
    // line:
    //
    // showBandAlpha(" b");

    $cfa = & getController() ;

    $arrangementObj ect = & $cfa->getObject("McA rrangements",
    "showBandAlpha" );
    $selectObject = & $cfa->getObject("Ext eriorSelect",
    "showBandAlpha" );


    // 08-21-06 - for the sake of speed, we only want to make this call
    to the database once,
    // so we get all the names of all the music bands and store it in a
    static array. We will
    // probably all this function 36 times on one page - once for each
    letter, and then for
    // each number 0-9. Rather than calling the database 36 times for
    the same information,
    // we call it once and store the info in a static array.
    static $bandNamesArray ;

    if (!is_array($ban dNamesArray)) {
    $selectObject->begin("showBan dAlpha");
    $selectObject->setDatastoreOb ject();
    $selectObject->setQueryObject ("GetAllOfType" );
    $selectObject->setInfoToBeSou ght("weblogPage s");
    $selectObject->getInfo("noLim it");
    $howMany =$selectObject->getCountOfRetu rn();

    for ($i=0; $i < $howMany; $i++) {
    $row = $selectObject->getRow();
    $bandNamesArray[] = $row;
    }
    }


    // 08-21-06 - this function was first written so as to match the
    first letter of a
    // string. We then got a request to match strings like "blues",
    "punk", and "grunge".
    $lengthOfCharsW eWantToMatch = strlen($firstFe wCharsWeWantToM atch);

    if (is_array($band NamesArray)) {
    // 08-21-06 - now we are going to loop through the array that
    holds all the band
    // names and we are going to pick out the ones whose begining
    matches the letter
    // or string that we are suppose to match against. For instance,
    a music band with
    // the name "Amazons" will be picked out if the designer has put
    the line
    // showBandAlpha(" a") on the page somewhere.
    for ($i=0; $i < count($bandName sArray); $i++) {
    $row = $bandNamesArray[$i];
    $val = $row[$fieldToGet];

    $beginningOfStr ingFromEntry = substr($val, 0,
    $lengthOfCharsW eWantToMatch);
    if (stristr($begin ningOfStringFro mEntry,
    $firstFewCharsW eWantToMatch)) {
    $arrangementObj ect->setEntry($row) ;
    $cfa->command("GetAr rangement", "listBandsToEdi t");
    }
    }
    } else {
    $cfa->error("In showBandAlpha we were not able to get an array of
    band names.", "showBandAlpha" );
    }

    // 08-11-06 - the database calls are taking too much memory. We need

    // to close it down.
    $selectObject->close();
    }



    ?>

  • Andy Hassall

    #2
    Re: my code is too slow, how do I speed it up?

    On 21 Aug 2006 15:50:59 -0700, "lawrence k" <lkrubner@geoci ties.comwrote:
    >The following function is way too slow. If anyone has any suggestions
    >about how to speed it up, I'd be grateful for them.
    Nobody can run your code as posted, so you have to do some work first. Find
    which bits of your code are slow, and start drilling down into the definitions.

    You can time sections of code with http://uk.php.net/microtime

    Or perhaps:



    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Jonathan

      #3
      Re: my code is too slow, how do I speed it up?

      lawrence k wrote:
      The following function is way too slow. If anyone has any suggestions
      about how to speed it up, I'd be grateful for them. We have to call
      this function 36 times on one page, and I think each time it takes half
      a second, so it adds up to maybe 18 seconds, which is a lot when you're
      showing software to a client. The reponse we get is "Why is it so
      slow?"
      <cut>php script(/cut>

      Lawrence,

      My guess is that your problems are in the database related queries and
      the way you prefer to execute and handle them.
      I prefer doing things in the database, as they are made for selecting,
      sorting and organizing. They are probably more memory and speed
      efficient than doing it in your own code.

      In my opinion it is useless to query all bands (the result set will
      probably be the whole table of bands) and then select the bands
      starting with a certain letter using you program logic.

      If you want to be sure you can write a script of course which times the
      display and execution of 36 or so queries to get all characters from the
      database and group them per character or query all the bands and display
      them in groups using your program code. My guess is that your program
      logic will consume (much) more time as you have to check whether the
      first character of the current record differs from the one before.

      Perhaps it is also a good idea to spend some time on the way you
      organize your database and define it's indexes, as the latter can really
      improve the speed of your queries. Holding all the data in one big table
      is also a bad idea, normalize your data so to minimize duplicate data in
      your database, this will also improve indexing.

      A few examples of search examples using the (my)SQL syntax:

      example 1:

      SELECT * FROM bands WHERE LOWER(bandname) LIKE 'a';

      The above example will simply select every band starting with a lower or
      capital a.

      example 2:

      BANDS table:
      id_band
      bandname
      id_genre

      GENRE table:
      id_genre
      genrename

      If you have normalized your tables (for information aboout normalization
      for databases, please use google, it willl give you plenty of good
      results) you would perhaps have a table which will hold all the genres
      and another for all the bands. Each band will have a genre associated
      with it in the bands table which can be linked to the genre table using
      queries like this:

      SELECT bands.bandname, genre.genrename
      FROM bands
      JOIN genre
      ON genre.id_genre = bands.id_genre
      WHERE genre.genrename = 'grunge' OR genre.genrename = 'punk';

      Above query will result in a two columns result set with the name of the
      band in one and the associated genre (either punk or grunge) in the
      second column.

      A third table could hold all the album data which should be related to
      the bands table, but can also be related to the genre table or some
      other table holding producers, recording studio or what so ever.

      There probably is a newsgroup/forum for your preferred database backend.
      If you are looking for MySQL groups news://mailing.database.mysql
      would be a good one to start.

      I know I went a bit OT, but who knows it might help you improve the
      speed of your code as well.

      Kind regards,

      Jonathan

      Comment

      • Richard Levasseur

        #4
        Re: my code is too slow, how do I speed it up?


        lawrence k wrote:
        The following function is way too slow. If anyone has any suggestions
        about how to speed it up, I'd be grateful for them. We have to call
        this function 36 times on one page, and I think each time it takes half
        a second, so it adds up to maybe 18 seconds, which is a lot when you're
        showing software to a client. The reponse we get is "Why is it so
        slow?"
        >
        >
        <?php
        >
        >
        function showBandAlpha($ firstFewCharsWe WantToMatch=fal se,
        $fieldToGet="cb Headline") {
        // 05-22-06 - I'm working with Hoyt on www.monkeyclaus.org, he wants
        to list all the
        // music bands, segmented by the first letter, so we need to all the
        bandnames as an array,
        // and then checked against the first letter. That is, he wants to
        be able to show all
        // the music bands whose name starts with the letter "A" by putting
        this line in his design:
        //
        // showBandAlpha(" a");
        //
        // and all the bands whose name starts with the letter "b" should
        appear wherever Darren puts the
        // line:
        //
        // showBandAlpha(" b");
        >
        $cfa = & getController() ;
        >
        $arrangementObj ect = & $cfa->getObject("McA rrangements",
        "showBandAlpha" );
        $selectObject = & $cfa->getObject("Ext eriorSelect",
        "showBandAlpha" );
        >
        >
        // 08-21-06 - for the sake of speed, we only want to make this call
        to the database once,
        // so we get all the names of all the music bands and store it in a
        static array. We will
        // probably all this function 36 times on one page - once for each
        letter, and then for
        // each number 0-9. Rather than calling the database 36 times for
        the same information,
        // we call it once and store the info in a static array.
        static $bandNamesArray ;
        >
        I think that you're going about caching the wrong way in this instance.
        The bands database could grow very, very large, and storing all of
        that in PHP could easily become prohibitive. But, if you're going to
        be selecting everything from the database every page load (a Bad Idea),
        then we'll go with that:

        Since you're going to be searching all the data in memory, you should
        store it in a structure that provides a quick way of searching (a tree
        or heap would be a good idea in this case). This can easily reduce
        your search time, per letter, from O(N) to O(log N). If you aren't
        familar with what that means, basically think of it as an expontial
        speedup.

        It will be easy to either write your own or find one.

        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Benchmark_Profi ler (Was Re: my code is too slow, how do I speed it up?)

          Andy Hassall wrote:
          <snip>Out of interest... I'm just curious, are you really using this
          package?

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          • Andy Hassall

            #6
            Re: Benchmark_Profi ler (Was Re: my code is too slow, how do I speed it up?)

            On 26 Aug 2006 13:28:33 -0700, "R. Rajesh Jeba Anbiah"
            <ng4rrjanbiah@r ediffmail.comwr ote:
            >Andy Hassall wrote:
            <snip>>
            Out of interest... I'm just curious, are you really using this
            >package?
            Nope; just looked like something the OP might want to try.

            --
            Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
            http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              Re: Benchmark_Profi ler (Was Re: my code is too slow, how do I speed it up?)

              Andy Hassall wrote:
              On 26 Aug 2006 13:28:33 -0700, "R. Rajesh Jeba Anbiah"
              <ng4rrjanbiah@r ediffmail.comwr ote:
              <snip>
              Out of interest... I'm just curious, are you really using this
              package?
              >
              Nope; just looked like something the OP might want to try.
              (Apologies for late response; had connectivity problem.) The
              script's method look unnecessary overkill to me. The solution based on
              ticks (though it won't work in Windows) <http://in2.php.net/declare>
              and further refinements by Chung Leong looks better than this one. I'm
              still after APD even though it's kinda dead.

              --
              <?php echo 'Just another PHP saint'; ?>
              Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

              Comment

              Working...