published on 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