Skip to content

Instantly share code, notes, and snippets.

@ju1ius
Created May 13, 2012 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ju1ius/2688777 to your computer and use it in GitHub Desktop.
Save ju1ius/2688777 to your computer and use it in GitHub Desktop.
Xpath descendant benchmark
<?php
require 'Benchmark/Timer.php';
$timer = new Benchmark_Timer();
$nb_iterations = 1000;
/**
* BOOTSTRAP
**/
$dom = @DOMDocument::loadHTMLFile('https://github.com/ju1ius/symfony/commit/48117d32ab40f84866ba66ec756ebdaa4268bcff');
$dom->recover = true;
$dom->strictErrorChecking = false;
$xpath = new DOMXpath($dom);
function get_class_query($class)
{
return "contains(concat(' ',normalize-space(@class),' '), ' $class ')";
}
// Queries unsing *[name() = '%s']
$div_q_1 = "*[name() = 'div' and " . get_class_query('diff-view') . "]";
$el_q_1 = "*[name() = 'td' and " . get_class_query('line_numbers') . "]";
$descendant_query = "//$div_q_1/descendant::$el_q_1";
//var_dump($descendant_query);
$descendant_shortcut_query = "//$div_q_1//$el_q_1";
//var_dump($descendant_shortcut_query);
// Queries unsing tag name
$div_q_2 = "div[".get_class_query('diff-view')."]";
$el_q_2 = "td[".get_class_query('line_numbers')."]";
$descendant_query_2 = "//$div_q_2/descendant::$el_q_2";
//var_dump($descendant_query_2);
$descendant_shortcut_query_2 = "//$div_q_2//$el_q_2";
//var_dump($descendant_shortcut_query_2);
/**
* TEST RUN
**/
$timer->start();
for ($i = 0; $i < $nb_iterations; $i++) {
$nodeset = $xpath->query($descendant_query);
}
var_dump($nodeset->length);
$timer->setMarker('descendant::');
for ($i = 0; $i < $nb_iterations; $i++) {
$nodeset = $xpath->query($descendant_shortcut_query);
}
var_dump($nodeset->length);
$timer->setMarker('//');
for ($i = 0; $i < $nb_iterations; $i++) {
$nodeset = $xpath->query($descendant_query_2);
}
var_dump($nodeset->length);
$timer->setMarker('descendant:: with tag');
for ($i = 0; $i < $nb_iterations; $i++) {
$nodeset = $xpath->query($descendant_shortcut_query_2);
}
var_dump($nodeset->length);
$timer->setMarker('// with tag');
printf("Memory: %s\n", memory_get_usage(true));
printf("Memory peak: %s\n", memory_get_peak_usage(true));
echo $timer->getOutput();
//echo $dom->saveHTML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment