|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2021-09-09 09:52 UTC] cmb@php.net
-Status: Open
+Status: Not a bug
-Assigned To:
+Assigned To: cmb
[2021-09-09 09:52 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 06:00:01 2025 UTC |
Description: ------------ Here are three sample queries Equivalent of the CSS `tr td.one` //tr//td[contains(concat(\' \', normalize-space(@class), \' \'), \' one \')] Equivalent of the CSS `td.one` //td[contains(concat(\' \', normalize-space(@class), \' \'), \' one \')] Equivalent of the CSS `tr td` //tr//td There's a test script below that uses these queries on a 1000x6 HTML table. I'd expect them all to take roughly the same amount of time to run. Test script: --------------- <?php $xml = '<table>'; for ($i = 0; $i < 1000; $i++) { $xml .= '<tr><td class="one">A</td><td class="two">B</td><td class="three">C</td><td class="four">D</td><td class="five">E</td><td class="six">F</td> </tr>'; } $xml .= '</table>'; $doc = new \DomDocument; $doc->loadXml($xml); $xpath = new \DomXpath($doc); $t1 = microtime(true); // tr td.one $xpath->query('//tr//td[contains(concat(\' \', normalize-space(@class), \' \'), \' one \')]'); $t2 = microtime(true); echo '<p>tr td.one: ' . ($t2 - $t1) . '</p>'; $t1 = microtime(true); // td.one $xpath->query('//td[contains(concat(\' \', normalize-space(@class), \' \'), \' one \')]'); $t2 = microtime(true); echo '<p>td.one: ' . ($t2 - $t1) . '</p>'; $t1 = microtime(true); // tr td $xpath->query('//tr//td'); $t2 = microtime(true); echo '<p>tr td: ' . ($t2 - $t1) . '</p>'; Expected result: ---------------- While I'd expect some variation in speed, the first expression seems unreasonably slow in comparison to the others. Actual result: -------------- tr td.one: 0.26365518569946 td.one: 0.0054380893707275 tr td: 0.00074887275695801 Finding all td elements by class name is fast Finding all td elements inside a td is fast Combining those, finding all td elements by class inside a tr is 50 times slower than just finding all td elements by class. Since the first expression is a combination of the other two, how can it be 50 times slower? It's clearly not the `concat` and `normalize-space` functions that are slowing it down because the second expression is fine.