Loading ...
Sorry, an error occurred while loading the content.

All about PHP Optimization Scripts

Expand Messages
  • Kazi Mohammad Ekram
    //This scripts are not my own. I collect it from below links to share it among us// 1. Multiple arguments with echo When you use echo, you probably use
    Message 1 of 6 , Oct 23, 2008
    • 0 Attachment

      //This scripts are not my own. I collect it from below links to share it among us//

      1. Multiple arguments with echo
      When you use echo, you probably use something like this:

      echo $variable1 . 'string1' . $variable2 . $variable3;

      But, you may be forgetting that echo can take multiple arguments. So you can write it like this:

      echo $variable1 , 'string1' , $variable2 , $variable3;

      Passing multiple arguments to echo is faster than joining the strings first, and then passing them to echo. I done a little test in PHP. I echoed 10 different strings 500 thousand times, firstly with the concatenation (joining) method, and then with the multiple arguments method. Here are my results:

      Time for concatenation method: 37.83755 seconds
      Time for multiple arguments method: 37.68789 seconds
      Time saved: 0.15966 seconds; 0.396%

      As you can see, the difference is very small. It is not worth going over all your old PHP scripts and changing the dots to commas, unless you are extremely desperate for speed. It is more a case of preference, than there being a best way.

      Oh, and another small tip (without it's own number): use echo instead of print - it's faster!

      2. Reduce Function Calls
      Now it may seem obvious, but many scripts have unnecessary calls to functions. Look at this:

      $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
      for($a = 0; $a < sizeof($array); $a++) {
      // Do something
      }

      How many times is the function sizeof() called? If you think once, you are wrong. It is actually called 21 times - once for every iteration. You should do the function call (or calculation) outside of the for loop, assign the value to a variable and use the variable in the for loop.

      I done another test. In each test, I had an array with 10,000 elements, and a for loop with 10,000 iterations. In the first test, I had sizeof($array) within the for loop - so it would be called 10,001 times. In the second test, I got the value sizeof($array) and assigned it to the variable $no. I then used $no in the for loop. Test 2 should be faster than test 1, because sizeof() is called 10,000 times less. Here are my results:

      Test 1 (sizeof() called 10, 001 times): 0.02784 seconds
      Test 2 (
      sizeof() called 1 time): 0.01278 seconds
      Time saved: 0.01506 seconds; 54.095%

      You can visit my blog for see more..

      References links are

      http://bitfilm.net/2007/08/24/tips-for-faster-php-scripts/

      http://bitfilm.net/2007/09/07/more-tips-for-faster-php-scripts/

      http://bitfilm.net/2007/11/27/even-more-tips-for-faster-php-scripts/

       
      Kazi Mohammad Ekram
      Junior Web Programmer
      W          : http://ekram.athost.net/

    • Arafat Rahman
      Thanks Mr, Ekram Really nice tips. Arafat Rahman http://arafatbd.net
      Message 2 of 6 , Oct 23, 2008
      • 0 Attachment
        Thanks Mr, Ekram
        Really nice tips.

        Arafat Rahman
        http://arafatbd.net
      • Harun
        Thanks Ekram vi, I will be great help   Kazi Harun Or Roshid Mob:+357-96351387 ... From: Kazi Mohammad Ekram To:
        Message 3 of 6 , Oct 23, 2008
        • 0 Attachment
          Thanks Ekram vi,
          I will be great help

           
          Kazi Harun Or Roshid
          Mob:+357-96351387


          ----- Original Message ----
          From: Kazi Mohammad Ekram <bluebird_ekram@...>
          To: phpexperts@yahoogroups.com
          Sent: Thursday, October 23, 2008 10:53:49
          Subject: [phpXperts] All about PHP Optimization Scripts


          //This scripts are not my own. I collect it from below links to share it among us//

          1. Multiple arguments with echo
          When you use echo, you probably use something like this:

          echo $variable1 . 'string1' . $variable2 . $variable3;

          But, you may be forgetting that echo can take multiple arguments. So you can write it like this:

          echo $variable1 , 'string1' , $variable2 , $variable3;

          Passing multiple arguments to echo is faster than joining the strings first, and then passing them to echo. I done a little test in PHP. I echoed 10 different strings 500 thousand times, firstly with the concatenation (joining) method, and then with the multiple arguments method. Here are my results:

          Time for concatenation method: 37.83755 seconds
          Time for multiple arguments method: 37.68789 seconds
          Time saved: 0.15966 seconds; 0.396%

          As you can see, the difference is very small. It is not worth going over all your old PHP scripts and changing the dots to commas, unless you are extremely desperate for speed. It is more a case of preference, than there being a best way.

          Oh, and another small tip (without it's own number): use echo instead of print - it's faster!

          2. Reduce Function Calls
          Now it may seem obvious, but many scripts have unnecessary calls to functions. Look at this:

          $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
          for($a = 0; $a < sizeof($array) ; $a++) {
          // Do something
          }

          How many times is the function sizeof() called? If you think once, you are wrong. It is actually called 21 times - once for every iteration. You should do the function call (or calculation) outside of the for loop, assign the value to a variable and use the variable in the for loop.

          I done another test. In each test, I had an array with 10,000 elements, and a for loop with 10,000 iterations. In the first test, I had sizeof($array) within the for loop - so it would be called 10,001 times. In the second test, I got the value sizeof($array) and assigned it to the variable $no. I then used $no in the for loop. Test 2 should be faster than test 1, because sizeof() is called 10,000 times less. Here are my results:

          Test 1 (sizeof() called 10, 001 times): 0.02784 seconds
          Test 2 (
          sizeof() called 1 time): 0.01278 seconds
          Time saved: 0.01506 seconds; 54.095%

          You can visit my blog for see more..

          References links are

          http://bitfilm. net/2007/ 08/24/tips- for-faster- php-scripts/

          http://bitfilm. net/2007/ 09/07/more- tips-for- faster-php- scripts/

          http://bitfilm. net/2007/ 11/27/even- more-tips- for-faster- php-scripts/

           
          Kazi Mohammad Ekram
          Junior Web Programmer
          W          : http://ekram. athost.net/



          Get your new Email address!
          Grab the Email name you've always wanted before someone else does!
        • sumn ahamed
          Thanks Mr.Ekram Vai for Mention of such types of Life Examles. New Email names for you! Get the Email name you've always wanted on the new @ymail and
          Message 4 of 6 , Oct 23, 2008
          • 0 Attachment
            Thanks Mr.Ekram Vai for Mention of such types of Life Examles.


            New Email addresses available on Yahoo!
            Get the Email name you've always wanted on the new @ymail and @rocketmail.
            Hurry before someone else does!
          • Rahat Bashir
            Thanks for the helpful tips. -- Rahat On Thu, Oct 23, 2008 at 1:53 PM, Kazi Mohammad Ekram
            Message 5 of 6 , Oct 26, 2008
            • 0 Attachment
              Thanks for the helpful tips.

              --
              Rahat

              On Thu, Oct 23, 2008 at 1:53 PM, Kazi Mohammad Ekram <bluebird_ekram@...> wrote:


              //This scripts are not my own. I collect it from below links to share it among us//

              1. Multiple arguments with echo
              When you use echo, you probably use something like this:

              echo $variable1 . 'string1' . $variable2 . $variable3;

              But, you may be forgetting that echo can take multiple arguments. So you can write it like this:

              echo $variable1 , 'string1' , $variable2 , $variable3;

              Passing multiple arguments to echo is faster than joining the strings first, and then passing them to echo. I done a little test in PHP. I echoed 10 different strings 500 thousand times, firstly with the concatenation (joining) method, and then with the multiple arguments method. Here are my results:

              Time for concatenation method: 37.83755 seconds
              Time for multiple arguments method: 37.68789 seconds
              Time saved: 0.15966 seconds; 0.396%

              As you can see, the difference is very small. It is not worth going over all your old PHP scripts and changing the dots to commas, unless you are extremely desperate for speed. It is more a case of preference, than there being a best way.

              Oh, and another small tip (without it's own number): use echo instead of print - it's faster!

              2. Reduce Function Calls
              Now it may seem obvious, but many scripts have unnecessary calls to functions. Look at this:

              $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
              for($a = 0; $a < sizeof($array); $a++) {
              // Do something
              }

              How many times is the function sizeof() called? If you think once, you are wrong. It is actually called 21 times - once for every iteration. You should do the function call (or calculation) outside of the for loop, assign the value to a variable and use the variable in the for loop.

              I done another test. In each test, I had an array with 10,000 elements, and a for loop with 10,000 iterations. In the first test, I had sizeof($array) within the for loop - so it would be called 10,001 times. In the second test, I got the value sizeof($array) and assigned it to the variable $no. I then used $no in the for loop. Test 2 should be faster than test 1, because sizeof() is called 10,000 times less. Here are my results:

              Test 1 (sizeof() called 10, 001 times): 0.02784 seconds
              Test 2 (
              sizeof() called 1 time): 0.01278 seconds
              Time saved: 0.01506 seconds; 54.095%

              You can visit my blog for see more..

              References links are

              http://bitfilm.net/2007/08/24/tips-for-faster-php-scripts/

              http://bitfilm.net/2007/09/07/more-tips-for-faster-php-scripts/

              http://bitfilm.net/2007/11/27/even-more-tips-for-faster-php-scripts/

               
              Kazi Mohammad Ekram
              Junior Web Programmer
              W          : http://ekram.athost.net/




              --
              Rahat Bashir
              Dhaka, Bangladesh
              +8801715812079
            • 9el
              ... share it ... arguments. So you ... strings ... PHP. I * ... arguments method. ... going over ... you are ... than there ... instead of ... 16, 17, ...
              Message 6 of 6 , Nov 1, 2008
              • 0 Attachment
                > On Thu, Oct 23, 2008 at 1:53 PM, Kazi Mohammad Ekram <
                > bluebird_ekram@...> wrote:
                >
                > >
                > > *//This scripts are not my own. I collect it from below links to
                share it
                > > among us//*
                > >
                > > *1. Multiple arguments with echo*
                > > When you use echo, you probably use something like this:
                > >
                > > echo $variable1 . 'string1' . $variable2 . $variable3;
                > >
                > > But, you may be forgetting that *echo* can take multiple
                arguments. So you
                > > can write it like this:
                > >
                > > echo $variable1 , 'string1' , $variable2 , $variable3;
                > >
                > > Passing multiple arguments to *echo *is faster than joining the
                strings
                > > first, and then passing them to *echo*. I done a little test in
                PHP. I *
                > > echo*ed 10 different strings 500 thousand times, firstly with the
                > > concatenation (joining) method, and then with the multiple
                arguments method.
                > > Here are my results:
                > >
                > > Time for concatenation method: 37.83755 seconds
                > > Time for multiple arguments method: 37.68789 seconds
                > > Time saved: 0.15966 seconds; 0.396%
                > >
                > > As you can see, the difference is very small. It is not worth
                going over
                > > all your old PHP scripts and changing the dots to commas, unless
                you are
                > > extremely desperate for speed. It is more a case of preference,
                than there
                > > being a best way.
                > >
                > > Oh, and another small tip (without it's own number): use *echo*
                instead of
                > > *print* - it's faster!
                > >
                > > *2. Reduce Function Calls**
                > > *Now it may seem obvious, but many scripts have unnecessary calls to
                > > functions. Look at this:
                > >
                > > $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
                16, 17,
                > > 18, 19, 20);
                > > for($a = 0; $a < sizeof($array); $a++) {
                > > // Do something
                > > }
                > >
                > > How many times is the function *sizeof()* called? If you think
                once, you
                > > are wrong. It is actually called 21 times - once for every
                iteration. You
                > > should do the function call (or calculation) outside of the *for*
                loop,
                > > assign the value to a variable and use the variable in the *for* loop.
                > >
                > > I done another test. In each test, I had an array with 10,000
                elements, and
                > > a *for *loop with 10,000 iterations. In the first test, I had *
                > > sizeof($array)* within the *for* loop - so it would be called 10,001
                > > times. In the second test, I got the value *sizeof($array)* and
                assigned
                > > it to the variable *$no*. I then used *$no* in the *for *loop. Test 2
                > > should be faster than test 1, because *sizeof()* is called 10,000
                times
                > > less. Here are my results:
                > >
                > > Test 1 (*sizeof()* called 10, 001 times): 0.02784 seconds
                > > Test 2 (*sizeof() *called 1 time): 0.01278 seconds
                > > Time saved: 0.01506 seconds; 54.095%
                > > You can visit my blog for see more..
                > > http://ekram85.wordpress.com/
                > >
                > > References links are
                > >
                > > http://bitfilm.net/2007/08/24/tips-for-faster-php-scripts/
                > >
                > > http://bitfilm.net/2007/09/07/more-tips-for-faster-php-scripts/
                > >
                > > http://bitfilm.net/2007/11/27/even-more-tips-for-faster-php-scripts/
                > >
                > > *Kazi Mohammad Ekram*
                > > *Junior Web Programmer*
                > > *W* : http://ekram.athost.net/
                > --
                > Rahat Bashir
                > Dhaka, Bangladesh
                > +8801715812079
                >

                The optimization tips definitely help in large scale where u cant
                afford to waste any delay or burden on the server's processor than are
                necessary.
                In the second example if functions other than sizeof() is used, can
                cause huge difference. If not time be a factor then processor overload
                can.
              Your message has been successfully submitted and would be delivered to recipients shortly.