a weblog by Ed Eliot

Full of lovely web standards information and a large helping of PHP

This weblog is the online home of , a Senior Technical Manager at GCap Media living and working in London, United Kingdom.

Entries for July 2007

You are currently viewing posts from the archive. Click here to return to the latest posts.

  • PHP Tip: A quick way to measure the performance of code

    10 months, 6 days ago

    I just stumbled across some interesting comments in reply to a post about increasing PHP performance which suggest using the UNIX command line program "time" to provide a quick way to benchmark the performance of PHP code constructs within a terminal window.

    A Couple of Examples

    Here's a couple of quick tests I ran to compare the relative speed of two blocks of code which output text via the PHP "echo" command. One uses concatenation to output 3 separate strings via the "echo" command. The other echo's comma separated argument format.

    1. time php -r "for (\$i = 0; \$i < 1000000; \$i++) echo 'Test 1'.'Test 2'.'Test 3';" > /dev/null

    gave the following results:

    1. real 0m3.393s
    2. user 0m2.064s
    3. sys 0m1.316s
    1. time php -r "for (\$i = 0; \$i < 1000000; \$i++) echo 'Test 1','Test 2','Test 3';" > /dev/null

    gave the following results:

    1. real 0m8.232s
    2. user 0m4.225s
    3. sys 0m3.919s

    Numbers displayed are in seconds. Unexpectedly, as you can see from the timings above, the sting concatenation method proves to be more efficient taking less than half the time to run.

    Here's another which this time as expected shows that the "split" function, which has the overhead of regular expression matching support, is slower than "explode".

    1. time php -r "for (\$i = 0; \$i < 1000000; \$i++) split(';', 'Test 1;Test 2;Test 3');" > /dev/null

    gave the following results:

    1. real 0m3.694s
    2. user 0m3.621s
    3. sys 0m0.027s
    1. time php -r "for (\$i = 0; \$i < 1000000; \$i++) explode(';', 'Test 1;Test 2;Test 3');" > /dev/null

    gave the following results:

    1. real 0m1.866s
    2. user 0m1.837s
    3. sys 0m0.019s

    Formatting The Output

    Some versions of "time" allow you to specify an output format for the running time. The Mac version doesn't hence it's omission in the examples above.

    Things To Watch

    • You need to quote the PHP code.
    • You need to escape any $'s and double quotes within the PHP code.
  • bzr2zip - creates a zip from a Bazaar repository

    10 months, 11 days ago

    Here's a quick bash script I knocked together to automatically create a zip of the files contained in a Bazaar branch. It takes two parameters - the location of the branch (local, sftp or http) and the name of the zip file to create (minus the .zip extension).

    1. #! /bin/bash
    2. echo -e "\nbzr2zip - checks out Bazaar repository and creates zip of files (requires Bazaar)\n"
    3. if [ $# -eq 2 ]; then
    4. bzr co $1 $2
    5. if [ $? -eq 0 ]; then
    6. rm -fr $2/.bzr
    7. zip -rm $2 $2
    8. echo -e "\nCreated: $2.zip\n"
    9. fi
    10. else
    11. echo "Usage: ./bzr2zip branch-url zip-filename"
    12. fi

    Download a plain text version of the script.

    You can try it out on one of the branches in my public repository.

    My bash scripting skills aren't up to much so let me know if there's a better way to write anything contained in the script or think it's missing functionality that should be there.