published about 1 year ago (27.03.2007 08:06)

PHP: a quick SimpleXML benchmark

update: needs work to become comparable with the ruby benchmark posted earlier.

as an update to the ruby xml performance test, some info for PHPs standard way of parsing XML, SimpleXML. since it uses libxml2 internally, the results are more comparable to ruby’s libxml2 parser, although not identical … (i don’t yet understand where the factor 10 difference in xpath comes from).

loading an xml file

file size ms
10KB 0.71
100KB 5.88
1.6MB 120.97

simple xpath expression

file size ms
10KB 0.75
100KB 6.39
1.6MB 79.52

the test code

<?php

function benchmark($function)
{
   $start = microtime(true);
   for ($i=0; $i<10; $i++) {
      // execute the anonymous function ("yield")
      $function();
   }
   echo ((microtime(true) - $start) / 10) * 1000;
   echo "\n";
} 

// put file into filesystem cache (hope this works)
file_get_contents('products.xml');
/**
 * info: using create_function does not change performance 
 * (in comparison with executing the code in a more traditional php way)
 */

// loading xml into an object
benchmark(
   create_function(
      // function arguments as string
      '',
      // method body as string
      '$doc = simplexml_load_file("products.xml");'
   )  
); 

benchmark(
   create_function(
      // function arguments as string
      '',
      // method body as string
      '$doc = simplexml_load_file("products.xml");
       foreach ($doc->xpath("//articles/article/shortdesc") as $node) {
          //echo $node;
       }'
   )
);

?>

Posted in ,  | Tags , , ,  | no comments

published about 1 year ago (27.03.2007 07:43)

extending PHP's SimpleXML

PHP’s XML workhorse, SimpleXML can be extended. you can supply a class of your own for PHP to wrap all XML nodes instead of the default SimpleXMLElement. do this by passing the desired class name as a string to the simplexml_load_(file|string) methods:

<?php

class MySimpleXMLElement extends SimpleXMLElement
{     
   // extending parent method
   public function xpath($path)
   {  
      echo "evaluating the following xpath expression: $path\n";
      $result = parent::xpath($path);
      echo "found " . sizeof($result) . " nodes";
      return $result;
   }
}

$doc = simplexml_load_file('products.xml', 'MySimpleXMLElement');
// calling an extended method
$nodes = $doc->xpath('//articles/article/shortdesc');
echo "\n";

// calling a parent method
echo $doc->root->getName();
echo "\n";

?>

i didn’t poke around in the internals of the parent class too much, no idea how well that would. on the other hand, in many cases it might be a better solution to delegate to SimpleXML instead of inheriting and extending from it.

Posted in ,  | Tags , ,  | no comments

published over 2 years ago (21.05.2006 10:49)

going to RailsConf Europe!

rails_conf << phillip

i’ll be in london for a good week in september, mainly to attend the first european conference held on ruby on rails.

i’m excited about meeting the people behind ruby/rails in person, and to experience the rails community! from what i’ve learnt by reading blogs, books, documentation, emailing, viewing videos and listening to podcasts, and visiting sites created in rails i believe they are the most “human” and broad-minded of all programming communities i know. so i believe it will be a pleasant and fun experience, too. beside the obvious – getting a much deeper insight into the tech side of things and learning about rails’ future.

another reason i’m going is to commit myself more to the technology. having been a php guy all my developer life, today my brain says “use rails, it’s superior”, but habit and some lazy parts of my brain sometimes still respond “hey why bother – you already know one very powerful web development environment really well, and have invested a lot in it”. i believe the experience of attending a rails conference will provide a breakthrough and convince all of me, and only a warm and fuzzy feeling will be left.

since i’ve been to london only once, i’ll stay there more than a week. the conference is on the 14th/15th, but i’ll be in town from the weekend before the conference to the weekend after it (9th-17th). i’m looking forward to being in london again, too. i’m really curious – the last time i went was in the early 90s.

Posted in , ,  | Tags , , , , , , , ,