3v4l.org

run code in 500+ 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; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1fFB3
function name:  (null)
number of ops:  94
compiled vars:  !0 = $nativeDOM, !1 = $s, !2 = $nativeDOMNormalized, !3 = $dom3, !4 = $importedXmlString
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   16     0  E >   INIT_FCALL                                                   'define'
          1        SEND_VAL                                                     'NUMBER_OF_ITEMS'
          2        SEND_VAL                                                     200
          3        DO_ICALL                                                     
   17     4        INIT_FCALL                                                   'define'
          5        SEND_VAL                                                     'XML_NAME_SPACE'
          6        SEND_VAL                                                     'http%3A%2F%2Fexample.com%2Fxml'
          7        DO_ICALL                                                     
   19     8        FETCH_CONSTANT                                       ~7      'NUMBER_OF_ITEMS'
          9        ROPE_INIT                                         3  ~10     'Testing+with+'
         10        CAST                                              4  ~8      ~7
         11        ROPE_ADD                                          1  ~10     ~10, ~8
         12        ROPE_END                                          2  ~9      ~10, '+items'
         13        CONCAT                                               ~12     ~9, '%0A'
         14        ECHO                                                         ~12
   21    15        ECHO                                                         'Generated+DOM%E2%80%A6+'
   22    16        INIT_FCALL_BY_NAME                                           'generate_random_dom'
         17        DO_FCALL                                          0  $13     
         18        ASSIGN                                                       !0, $13
   23    19        INIT_FCALL                                                   'microtime'
         20        SEND_VAL                                                     <true>
         21        DO_ICALL                                             $15     
         22        ASSIGN                                                       !1, $15
   24    23        INIT_FCALL_BY_NAME                                           'iterate_with_xpath'
         24        SEND_VAR_EX                                                  !0
         25        DO_FCALL                                          0          
   25    26        INIT_FCALL                                                   'number_format'
         27        INIT_FCALL                                                   'microtime'
         28        SEND_VAL                                                     <true>
         29        DO_ICALL                                             $18     
         30        SUB                                                  ~19     $18, !1
         31        SEND_VAL                                                     ~19
         32        SEND_VAL                                                     5
         33        DO_ICALL                                             $20     
         34        CONCAT                                               ~21     $20, '+seconds'
         35        CONCAT                                               ~22     ~21, '%0A'
         36        ECHO                                                         ~22
   27    37        ECHO                                                         'Generated+DOM+with+normalizeDocument%28%29%E2%80%A6+'
   28    38        INIT_FCALL_BY_NAME                                           'generate_random_dom'
         39        DO_FCALL                                          0  $23     
         40        ASSIGN                                                       !2, $23
   29    41        INIT_FCALL                                                   'microtime'
         42        SEND_VAL                                                     <true>
         43        DO_ICALL                                             $25     
         44        ASSIGN                                                       !1, $25
   30    45        INIT_METHOD_CALL                                             !2, 'normalizeDocument'
         46        DO_FCALL                                          0          
   31    47        INIT_FCALL_BY_NAME                                           'iterate_with_xpath'
         48        SEND_VAR_EX                                                  !2
         49        DO_FCALL                                          0          
   32    50        INIT_FCALL                                                   'number_format'
         51        INIT_FCALL                                                   'microtime'
         52        SEND_VAL                                                     <true>
         53        DO_ICALL                                             $29     
         54        SUB                                                  ~30     $29, !1
         55        SEND_VAL                                                     ~30
         56        SEND_VAL                                                     5
         57        DO_ICALL                                             $31     
         58        CONCAT                                               ~32     $31, '+seconds'
         59        CONCAT                                               ~33     ~32, '%0A'
         60        ECHO                                                         ~33
   34    61        ECHO                                                         'Export+and+re-import+DOM%E2%80%A6+'
   35    62        INIT_FCALL_BY_NAME                                           'generate_random_dom'
         63        DO_FCALL                                          0  $34     
         64        ASSIGN                                                       !3, $34
   36    65        INIT_FCALL                                                   'microtime'
         66        SEND_VAL                                                     <true>
         67        DO_ICALL                                             $36     
         68        ASSIGN                                                       !1, $36
   37    69        NEW                                                  $38     'DOMDocument'
         70        SEND_VAL_EX                                                  '1.0'
         71        SEND_VAL_EX                                                  'UTF-8'
         72        DO_FCALL                                          0          
         73        ASSIGN                                                       !4, $38
   38    74        INIT_METHOD_CALL                                             !4, 'loadXML'
         75        INIT_METHOD_CALL                                             !3, 'saveXML'
         76        DO_FCALL                                          0  $41     
         77        SEND_VAR_NO_REF_EX                                           $41
         78        DO_FCALL                                          0          
   39    79        INIT_FCALL_BY_NAME                                           'iterate_with_xpath'
         80        SEND_VAR_EX                                                  !4
         81        DO_FCALL                                          0          
   40    82        INIT_FCALL                                                   'number_format'
         83        INIT_FCALL                                                   'microtime'
         84        SEND_VAL                                                     <true>
         85        DO_ICALL                                             $44     
         86        SUB                                                  ~45     $44, !1
         87        SEND_VAL                                                     ~45
         88        SEND_VAL                                                     5
         89        DO_ICALL                                             $46     
         90        CONCAT                                               ~47     $46, '+seconds'
         91        CONCAT                                               ~48     ~47, '%0A'
         92        ECHO                                                         ~48
   92    93      > RETURN                                                       1

Function iterate_with_xpath:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/1fFB3
function name:  iterate_with_xpath
number of ops:  23
compiled vars:  !0 = $dom, !1 = $xpath, !2 = $item
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   47     0  E >   RECV                                                 !0      
   49     1        NEW                                                  $3      'DOMXPath'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
          4        ASSIGN                                                       !1, $3
   50     5        INIT_METHOD_CALL                                             !1, 'registerNamespace'
          6        SEND_VAL_EX                                                  'listElement'
          7        FETCH_CONSTANT                                       ~6      'XML_NAME_SPACE'
          8        SEND_VAL_EX                                                  ~6
          9        DO_FCALL                                          0          
   53    10        INIT_METHOD_CALL                                             !1, 'query'
         11        SEND_VAL_EX                                                  '%2F%2FlistElement%3Aalpha'
         12        SEND_VAL_EX                                                  null
         13        FETCH_CONSTANT                                       ~8      'XML_NAME_SPACE'
         14        SEND_VAL_EX                                                  ~8
         15        DO_FCALL                                          0  $9      
         16      > FE_RESET_R                                           $10     $9, ->21
         17    > > FE_FETCH_R                                                   $10, !2, ->21
   54    18    >   INIT_METHOD_CALL                                             !2, 'C14N'
         19        DO_FCALL                                          0          
   53    20      > JMP                                                          ->17
         21    >   FE_FREE                                                      $10
   56    22      > RETURN                                                       null

End of function iterate_with_xpath

Function generate_random_dom:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 16
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 16
Branch analysis from position: 52
Branch analysis from position: 16
filename:       /in/1fFB3
function name:  generate_random_dom
number of ops:  56
compiled vars:  !0 = $dom, !1 = $list, !2 = $i, !3 = $listItem, !4 = $subElement
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   73     0  E >   NEW                                                  $5      'DOMDocument'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $5
   74     3        ASSIGN_OBJ                                                   !0, 'formatOutput'
          4        OP_DATA                                                      <true>
   77     5        INIT_METHOD_CALL                                             !0, 'createElementNS'
          6        FETCH_CONSTANT                                       ~9      'XML_NAME_SPACE'
          7        SEND_VAL_EX                                                  ~9
          8        SEND_VAL_EX                                                  'listElement'
          9        DO_FCALL                                          0  $10     
         10        ASSIGN                                                       !1, $10
   78    11        INIT_METHOD_CALL                                             !0, 'appendChild'
         12        SEND_VAR_EX                                                  !1
         13        DO_FCALL                                          0          
   80    14        ASSIGN                                                       !2, 0
         15      > JMP                                                          ->49
   82    16    >   INIT_METHOD_CALL                                             !0, 'createElementNS'
         17        FETCH_CONSTANT                                       ~14     'XML_NAME_SPACE'
         18        SEND_VAL_EX                                                  ~14
         19        SEND_VAL_EX                                                  'listItem'
         20        DO_FCALL                                          0  $15     
         21        ASSIGN                                                       !3, $15
   83    22        INIT_METHOD_CALL                                             !3, 'setAttribute'
         23        SEND_VAL_EX                                                  'index'
         24        SEND_VAR_EX                                                  !2
         25        DO_FCALL                                          0          
   84    26        INIT_METHOD_CALL                                             !1, 'appendChild'
         27        SEND_VAR_EX                                                  !3
         28        DO_FCALL                                          0          
   86    29        INIT_METHOD_CALL                                             !0, 'createElementNS'
         30        FETCH_CONSTANT                                       ~19     'XML_NAME_SPACE'
         31        SEND_VAL_EX                                                  ~19
         32        SEND_VAL_EX                                                  'alpha'
         33        DO_FCALL                                          0  $20     
         34        ASSIGN                                                       !4, $20
   87    35        INIT_METHOD_CALL                                             !4, 'appendChild'
         36        INIT_METHOD_CALL                                             !0, 'createTextNode'
         37        INIT_FCALL                                                   'uniqid'
         38        SEND_VAL                                                     'a'
         39        SEND_VAL                                                     <true>
         40        DO_ICALL                                             $22     
         41        SEND_VAR_NO_REF_EX                                           $22
         42        DO_FCALL                                          0  $23     
         43        SEND_VAR_NO_REF_EX                                           $23
         44        DO_FCALL                                          0          
   88    45        INIT_METHOD_CALL                                             !3, 'appendChild'
         46        SEND_VAR_EX                                                  !4
         47        DO_FCALL                                          0          
   80    48        PRE_INC                                                      !2
         49    >   FETCH_CONSTANT                                       ~27     'NUMBER_OF_ITEMS'
         50        IS_SMALLER                                                   !2, ~27
         51      > JMPNZ                                                        ~28, ->16
   91    52    >   VERIFY_RETURN_TYPE                                           !0
         53      > RETURN                                                       !0
   92    54*       VERIFY_RETURN_TYPE                                           
         55*     > RETURN                                                       null

End of function generate_random_dom

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
164.6 ms | 1378 KiB | 19 Q