3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * This code demonstrates the performance difference when calling * DOMElement::C14N() on generated DOMDocument versus on a DOMDocument that was * loaded from a string. * * Calling DOMDocument::normalizeDocument() doesn't make any difference. * * Output example: * Testing with 500 items * Native DOM (not normalized)… 2.55357 seconds * Native DOM (normalized)… 2.52199 seconds * Export and re-import DOM… 0.06444 seconds */ define('NUMBER_OF_ITEMS', 200); define('XML_NAME_SPACE', 'http://example.com/xml'); echo sprintf('Testing with %d items', NUMBER_OF_ITEMS) . PHP_EOL; echo 'Generated DOM… '; $nativeDOM = generate_random_dom(); $s = microtime(true); iterate_with_xpath($nativeDOM); echo number_format(microtime(true) - $s, 5) . ' seconds' . PHP_EOL; echo 'Generated DOM with normalizeDocument()… '; $nativeDOMNormalized = generate_random_dom(); $s = microtime(true); $nativeDOMNormalized->normalizeDocument(); iterate_with_xpath($nativeDOMNormalized); echo number_format(microtime(true) - $s, 5) . ' seconds' . PHP_EOL; echo 'Export and re-import DOM… '; $dom3 = generate_random_dom(); $s = microtime(true); $importedXmlString = new \DOMDocument('1.0', 'UTF-8'); $importedXmlString->loadXML($dom3->saveXML()); iterate_with_xpath($importedXmlString); echo number_format(microtime(true) - $s, 5) . ' seconds' . PHP_EOL; /** * Iterate over items' content and call C14N * * @param \DOMDocument $dom */ function iterate_with_xpath(\DOMDocument $dom): void { $xpath = new \DOMXPath($dom); $xpath->registerNamespace('listElement', XML_NAME_SPACE); /** @var \DOMElement $item */ foreach($xpath->query('//listElement:alpha', null, XML_NAME_SPACE) as $item) { $item->C14N(); } } /** * Generate random DOMDocument * * Example: * <listElement> * <listItem index="0"> * <alpha>random value here</alpha> * </listItem> * ... * </listElement> * * @return \DOMDocument */ function generate_random_dom(): \DOMDocument { $dom = new \DOMDocument(); $dom->formatOutput = true; /** @var \DOMElement $list */ $list = $dom->createElementNS(XML_NAME_SPACE, 'listElement'); $dom->appendChild($list); for($i = 0; $i < NUMBER_OF_ITEMS; $i++) { /** @var \DOMElement $listItem */ $listItem = $dom->createElementNS(XML_NAME_SPACE, 'listItem'); $listItem->setAttribute('index', $i); $list->appendChild($listItem); $subElement = $dom->createElementNS(XML_NAME_SPACE, 'alpha'); $subElement->appendChild($dom->createTextNode(uniqid('a', true))); $listItem->appendChild($subElement); } return $dom; }
Output for 7.3.12
Testing with 200 items Generated DOM… 0.20604 seconds Generated DOM with normalizeDocument()… 0.20983 seconds Export and re-import DOM… 0.01547 seconds
Output for 7.3.11
Testing with 200 items Generated DOM… 0.22538 seconds Generated DOM with normalizeDocument()… 0.22504 seconds Export and re-import DOM… 0.01869 seconds
Output for 7.3.10
Testing with 200 items Generated DOM… 0.22305 seconds Generated DOM with normalizeDocument()… 0.22406 seconds Export and re-import DOM… 0.01856 seconds
Output for 7.3.9
Testing with 200 items Generated DOM… 0.20290 seconds Generated DOM with normalizeDocument()… 0.19878 seconds Export and re-import DOM… 0.01555 seconds
Output for 7.3.8
Testing with 200 items Generated DOM… 0.19321 seconds Generated DOM with normalizeDocument()… 0.19848 seconds Export and re-import DOM… 0.01574 seconds
Output for 7.3.7
Testing with 200 items Generated DOM… 0.19785 seconds Generated DOM with normalizeDocument()… 0.19876 seconds Export and re-import DOM… 0.01547 seconds
Output for 7.3.6
Testing with 200 items Generated DOM… 0.22054 seconds Generated DOM with normalizeDocument()… 0.19795 seconds Export and re-import DOM… 0.01544 seconds
Output for 7.3.5
Testing with 200 items Generated DOM… 0.20355 seconds Generated DOM with normalizeDocument()… 0.21193 seconds Export and re-import DOM… 0.01540 seconds
Output for 7.3.4
Testing with 200 items Generated DOM… 0.20100 seconds Generated DOM with normalizeDocument()… 0.19821 seconds Export and re-import DOM… 0.01550 seconds
Output for 7.3.3
Testing with 200 items Generated DOM… 0.19685 seconds Generated DOM with normalizeDocument()… 0.19943 seconds Export and re-import DOM… 0.01558 seconds
Output for 7.3.2
Testing with 200 items Generated DOM… 0.19514 seconds Generated DOM with normalizeDocument()… 0.19332 seconds Export and re-import DOM… 0.01663 seconds
Output for 7.3.1
Testing with 200 items Generated DOM… 0.20014 seconds Generated DOM with normalizeDocument()… 0.19919 seconds Export and re-import DOM… 0.01532 seconds
Output for 7.3.0
Testing with 200 items Generated DOM… 0.19895 seconds Generated DOM with normalizeDocument()… 0.19876 seconds Export and re-import DOM… 0.01612 seconds
Output for 7.2.24
Testing with 200 items Generated DOM… 0.20153 seconds Generated DOM with normalizeDocument()… 0.21146 seconds Export and re-import DOM… 0.02004 seconds
Output for 7.2.23
Testing with 200 items Generated DOM… 0.19786 seconds Generated DOM with normalizeDocument()… 0.19695 seconds Export and re-import DOM… 0.01552 seconds
Output for 7.2.22
Testing with 200 items Generated DOM… 0.19615 seconds Generated DOM with normalizeDocument()… 0.19659 seconds Export and re-import DOM… 0.01728 seconds
Output for 7.2.21
Testing with 200 items Generated DOM… 0.19950 seconds Generated DOM with normalizeDocument()… 0.19849 seconds Export and re-import DOM… 0.01549 seconds
Output for 7.2.20
Testing with 200 items Generated DOM… 0.19879 seconds Generated DOM with normalizeDocument()… 0.19713 seconds Export and re-import DOM… 0.01637 seconds
Output for 7.2.19
Testing with 200 items Generated DOM… 0.19676 seconds Generated DOM with normalizeDocument()… 0.19832 seconds Export and re-import DOM… 0.01554 seconds
Output for 7.2.18
Testing with 200 items Generated DOM… 0.19727 seconds Generated DOM with normalizeDocument()… 0.19896 seconds Export and re-import DOM… 0.01532 seconds
Output for 7.2.17
Testing with 200 items Generated DOM… 0.19934 seconds Generated DOM with normalizeDocument()… 0.19857 seconds Export and re-import DOM… 0.01551 seconds
Output for 7.2.16
Testing with 200 items Generated DOM… 0.20067 seconds Generated DOM with normalizeDocument()… 0.20028 seconds Export and re-import DOM… 0.01560 seconds
Output for 7.2.15
Testing with 200 items Generated DOM… 0.20460 seconds Generated DOM with normalizeDocument()… 0.19902 seconds Export and re-import DOM… 0.01555 seconds
Output for 7.2.14
Testing with 200 items Generated DOM… 0.23355 seconds Generated DOM with normalizeDocument()… 0.23121 seconds Export and re-import DOM… 0.01859 seconds
Output for 7.2.13
Testing with 200 items Generated DOM… 0.22842 seconds Generated DOM with normalizeDocument()… 0.22645 seconds Export and re-import DOM… 0.01896 seconds
Output for 7.2.12
Testing with 200 items Generated DOM… 0.20294 seconds Generated DOM with normalizeDocument()… 0.19899 seconds Export and re-import DOM… 0.01592 seconds
Output for 7.2.11
Testing with 200 items Generated DOM… 0.22812 seconds Generated DOM with normalizeDocument()… 0.22359 seconds Export and re-import DOM… 0.01828 seconds
Output for 7.2.10
Testing with 200 items Generated DOM… 0.20245 seconds Generated DOM with normalizeDocument()… 0.19971 seconds Export and re-import DOM… 0.01559 seconds
Output for 7.2.9
Testing with 200 items Generated DOM… 0.19772 seconds Generated DOM with normalizeDocument()… 0.19779 seconds Export and re-import DOM… 0.01551 seconds
Output for 7.2.8
Testing with 200 items Generated DOM… 0.20027 seconds Generated DOM with normalizeDocument()… 0.19949 seconds Export and re-import DOM… 0.01580 seconds
Output for 7.2.7
Testing with 200 items Generated DOM… 0.19951 seconds Generated DOM with normalizeDocument()… 0.19649 seconds Export and re-import DOM… 0.01558 seconds
Output for 7.2.6
Testing with 200 items Generated DOM… 0.23639 seconds Generated DOM with normalizeDocument()… 0.22080 seconds Export and re-import DOM… 0.01951 seconds
Output for 7.2.5
Testing with 200 items Generated DOM… 0.20303 seconds Generated DOM with normalizeDocument()… 0.19944 seconds Export and re-import DOM… 0.01593 seconds
Output for 7.2.4
Testing with 200 items Generated DOM… 0.20390 seconds Generated DOM with normalizeDocument()… 0.20198 seconds Export and re-import DOM… 0.01557 seconds
Output for 7.2.3
Testing with 200 items Generated DOM… 0.19584 seconds Generated DOM with normalizeDocument()… 0.19754 seconds Export and re-import DOM… 0.01550 seconds
Output for 7.2.2
Testing with 200 items Generated DOM… 0.19947 seconds Generated DOM with normalizeDocument()… 0.19977 seconds Export and re-import DOM… 0.01579 seconds
Output for 7.2.1
Testing with 200 items Generated DOM… 0.19966 seconds Generated DOM with normalizeDocument()… 0.20001 seconds Export and re-import DOM… 0.01559 seconds
Output for 7.2.0
Testing with 200 items Generated DOM… 0.20401 seconds Generated DOM with normalizeDocument()… 0.20214 seconds Export and re-import DOM… 0.01575 seconds
Output for 7.1.33
Testing with 200 items Generated DOM… 0.22672 seconds Generated DOM with normalizeDocument()… 0.20499 seconds Export and re-import DOM… 0.01553 seconds
Output for 7.1.32
Testing with 200 items Generated DOM… 0.20055 seconds Generated DOM with normalizeDocument()… 0.19830 seconds Export and re-import DOM… 0.01541 seconds
Output for 7.1.31
Testing with 200 items Generated DOM… 0.19757 seconds Generated DOM with normalizeDocument()… 0.19796 seconds Export and re-import DOM… 0.01533 seconds
Output for 7.1.30
Testing with 200 items Generated DOM… 0.23099 seconds Generated DOM with normalizeDocument()… 0.23102 seconds Export and re-import DOM… 0.01861 seconds
Output for 7.1.29
Testing with 200 items Generated DOM… 0.19628 seconds Generated DOM with normalizeDocument()… 0.19974 seconds Export and re-import DOM… 0.01549 seconds
Output for 7.1.28
Testing with 200 items Generated DOM… 0.19954 seconds Generated DOM with normalizeDocument()… 0.20221 seconds Export and re-import DOM… 0.01566 seconds
Output for 7.1.27
Testing with 200 items Generated DOM… 0.19942 seconds Generated DOM with normalizeDocument()… 0.19982 seconds Export and re-import DOM… 0.01557 seconds
Output for 7.1.26
Testing with 200 items Generated DOM… 0.19924 seconds Generated DOM with normalizeDocument()… 0.19647 seconds Export and re-import DOM… 0.01548 seconds
Output for 7.1.25
Testing with 200 items Generated DOM… 0.19833 seconds Generated DOM with normalizeDocument()… 0.19665 seconds Export and re-import DOM… 0.01547 seconds

preferences:
49.04 ms | 527 KiB | 5 Q