Arrays + Performance surprise - curious for comments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    Arrays + Performance surprise - curious for comments

    Folks,
    A ng poster recently questioned their usage/creation of arrays and their
    correct syntax. I got the idea to performance test from a recent
    (excellent) PHP Tutorial article that was in Linux Format magazine (which
    dealt with performance).

    The original poster had a reply from someone who had said using
    $testArray[$keyName] was better (and proper) than $testArray["$keyName"]

    I *had* believed this to be correct... but... against my better judgement, I
    decided to test the assumption.

    I did a simple test to confirm - Basically, a loop that performed a
    conditional 'if' test a large number of times - One used the double quotes
    around the array key, one went without. Other than that, the rest was the
    same - the box was almost asleep too during the test with a single user and
    no cron jobs or other activity. To make sure, I ran the script twice, ten
    minutes apart.... and the results?

    Strangely, the test *with* the double quotes took half the time. We're
    talking about Test 1 taking 85seconds and test 2 taking 152seconds

    I'm curious on anything anyone can add to this - It got me thinking that if
    something is faster, is it right/recommended practice?

    My test script is below....

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet?

    <?
    // Perform a test $maxLoop number of times, count the number of seconds
    // it takes to complete the loop - Test 1 has the array element named
    // inside double quotes, Test 2 excludes the double quotes
    set_time_limit( 250);
    $testArray=$_EN V;
    $maxLoop=100000 ;
    /////////////////////// Test 1 //////////////////// This took 85seconds to
    complete
    $start=time();
    for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
    { foreach($testAr ray as $elementName=>$ elementValue)
    { if($testArray["$elementNa me"]=="HOST")
    { $thisHost="$ele mentValue"; }
    }
    }
    $end=time();
    $difference=$en d-$start;
    print("<br>Dura tion 1: $difference<hr> ");

    set_time_limit( 250);
    $testArray=$_EN V;
    $maxLoop=100000 ;
    /////////////////////// Test 2 //////////////////// This took 151 seconds to
    complete
    for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
    { foreach($testAr ray as $elementName=>$ elementValue)
    { if($testArray[$elementName]=="HOST")
    { $thisHost="$ele mentValue"; }
    }
    }
    $end=time();
    $difference=$en d-$start;
    print("<br>Dura tion 2: $difference<hr> ");
    ?>

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet?


  • Randell D.

    #2
    Re: Arrays + Performance surprise - curious for comments


    "Randell D." <you.can.email. me.at.randelld@ yahoo.com> wrote in message
    news:Brvbb.1007 470$ro6.1968382 5@news2.calgary .shaw.ca...[color=blue]
    > Folks,
    > A ng poster recently questioned their usage/creation of arrays and their
    > correct syntax. I got the idea to performance test from a recent
    > (excellent) PHP Tutorial article that was in Linux Format magazine (which
    > dealt with performance).
    >
    > The original poster had a reply from someone who had said using
    > $testArray[$keyName] was better (and proper) than $testArray["$keyName"]
    >
    > I *had* believed this to be correct... but... against my better judgement,[/color]
    I[color=blue]
    > decided to test the assumption.
    >
    > I did a simple test to confirm - Basically, a loop that performed a
    > conditional 'if' test a large number of times - One used the double quotes
    > around the array key, one went without. Other than that, the rest was the
    > same - the box was almost asleep too during the test with a single user[/color]
    and[color=blue]
    > no cron jobs or other activity. To make sure, I ran the script twice, ten
    > minutes apart.... and the results?
    >
    > Strangely, the test *with* the double quotes took half the time. We're
    > talking about Test 1 taking 85seconds and test 2 taking 152seconds
    >
    > I'm curious on anything anyone can add to this - It got me thinking that[/color]
    if[color=blue]
    > something is faster, is it right/recommended practice?
    >
    > My test script is below....
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is top-posting such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet?
    >
    > <?
    > // Perform a test $maxLoop number of times, count the number of seconds
    > // it takes to complete the loop - Test 1 has the array element named
    > // inside double quotes, Test 2 excludes the double quotes
    > set_time_limit( 250);
    > $testArray=$_EN V;
    > $maxLoop=100000 ;
    > /////////////////////// Test 1 //////////////////// This took 85seconds[/color]
    to[color=blue]
    > complete
    > $start=time();
    > for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
    > { foreach($testAr ray as $elementName=>$ elementValue)
    > { if($testArray["$elementNa me"]=="HOST")
    > { $thisHost="$ele mentValue"; }
    > }
    > }
    > $end=time();
    > $difference=$en d-$start;
    > print("<br>Dura tion 1: $difference<hr> ");
    >
    > set_time_limit( 250);
    > $testArray=$_EN V;
    > $maxLoop=100000 ;
    > /////////////////////// Test 2 //////////////////// This took 151 seconds[/color]
    to[color=blue]
    > complete
    > for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
    > { foreach($testAr ray as $elementName=>$ elementValue)
    > { if($testArray[$elementName]=="HOST")
    > { $thisHost="$ele mentValue"; }
    > }
    > }
    > $end=time();
    > $difference=$en d-$start;
    > print("<br>Dura tion 2: $difference<hr> ");
    > ?>
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is top-posting such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet?
    >
    >[/color]

    As 'BKDotCom' points out in another post - I got something wrong in my test
    8(...

    *cough cough*
    Your test 2 took 66 seconds.
    you didn't reinitialize $start after test1 compleated... therefore
    152 seconds is the total time your script ran.

    so:
    86 sec for test 1 (quotes)
    66 sec for test 2 (no quotes)
    152 sec: total

    but, bonus points to you for actually taking the time to research this.


    Comment

    • Allan Savolainen

      #3
      Re: Arrays + Performance surprise - curious for comments

      I tried following:

      <?php
      $maxLoop=100000 ;
      $iterations=20;

      $start=time();
      for ($i=0;$i<$itera tions;$i++){
      unset($test);
      for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
      {
      $test[]=1;
      }
      }
      $end=time();
      $difference=$en d-$start;
      print("<br>Dura tion 0: $difference<hr> ");

      $start=time();
      for ($i=0;$i<$itera tions;$i++){
      unset($test);
      for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
      {
      $test[$loopMany]=1;
      }
      }
      $end=time();
      $difference=$en d-$start;
      print("<br>Dura tion 1: $difference<hr> ");

      $start=time();
      set_time_limit( 250);
      for ($i=0;$i<$itera tions;$i++){
      unset($test);
      for($loopMany=0 ; $loopMany<$maxL oop; ++$loopMany)
      {
      $test["$loopMany"]=1;
      }
      }
      $end=time();
      $difference=$en d-$start;
      print("<br>Dura tion 2: $difference<hr> ");
      ?>

      and got these results:

      $test[] 18secs
      $test[$loopMany] 22secs
      $test["$loopMany"] 50secs

      I think the difference is due to how PHP handles quoted strings. PHP
      first has to parse the quoted string and replace all variables with
      their values (probably creates a temp string for this operation) and
      after that it can use the resulted string as array index. When
      variable is used directly PHP doesn't have to do this extra step.
      But I'm only guessing, I have no idea of the inner workings of PHP.

      - allan savolainen

      Comment

      • Zac Hester

        #4
        Re: Arrays + Performance surprise - curious for comments

        Allan Savolainen wrote:[color=blue]
        >
        > I think the difference is due to how PHP handles quoted strings. PHP
        > first has to parse the quoted string and replace all variables with
        > their values (probably creates a temp string for this operation) and
        > after that it can use the resulted string as array index. When
        > variable is used directly PHP doesn't have to do this extra step.
        > But I'm only guessing, I have no idea of the inner workings of PHP.
        >
        > - allan savolainen[/color]

        Allan,

        You're exactly right. Putting a variable in double-quotes is called
        string interpolation. It not only requires this extra step, but calls
        up a whole subset of parsing routines to break the string into
        constituent parts. (In some instances, it's even faster to concatenate
        static strings instead of relying on interpolation.) You should also
        run your test using all of the different quoting methods:

        $mykey = 'key';
        $array[]
        $array[key]
        $array['key']
        $array["key"]
        $array[$mykey]
        $array["$mykey"]

        [Additional Tests:]
        $k1 = 'k'; $k2 = 'e'; $k3 = 'y';
        $array["$k1$k2$k3"]
        $array[$k1.$k2.$k3]
        $array['k'.'e'.'y']

        You should find out that using single-quoted strings are slightly faster
        than the same double-quoted string. (It doesn't trip the interpolation
        system, but just assumes the string is a constant.)

        It's not all that hard to figure out what PHP is doing. The Zend
        language is pretty similar to C, so you should be able to browse the
        source code to PHP and get a good idea of "the inner workings of PHP."
        I do all the time just to figure out what the best way to do something
        is. At first, finding the code can be a little daunting (there's a
        couple hundred source files). But, a good text searching program will
        go a long way in finding the information you need. It might also help
        to do a little research on the Apache API, since PHP is pretty
        integrated into Apache for certain things (especially when you're using
        it as a module and not as a CGI binary).

        Does anybody know if heredocs are faster or slower than quoted strings?

        Take care,
        Zac

        Comment

        Working...