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; }
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        INIT_FCALL                                               'sprintf'
          9        SEND_VAL                                                 'Testing+with+%25d+items'
         10        FETCH_CONSTANT                                   ~7      'NUMBER_OF_ITEMS'
         11        SEND_VAL                                                 ~7
         12        DO_ICALL                                         $8      
         13        CONCAT                                           ~9      $8, '%0A'
         14        ECHO                                                     ~9
   21    15        ECHO                                                     'Generated+DOM%E2%80%A6+'
   22    16        INIT_FCALL_BY_NAME                                       'generate_random_dom'
         17        DO_FCALL                                      0  $10     
         18        ASSIGN                                                   !0, $10
   23    19        INIT_FCALL                                               'microtime'
         20        SEND_VAL                                                 <true>
         21        DO_ICALL                                         $12     
         22        ASSIGN                                                   !1, $12
   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                                         $15     
         30        SUB                                              ~16     $15, !1
         31        SEND_VAL                                                 ~16
         32        SEND_VAL                                                 5
         33        DO_ICALL                                         $17     
         34        CONCAT                                           ~18     $17, '+seconds'
         35        CONCAT                                           ~19     ~18, '%0A'
         36        ECHO                                                     ~19
   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  $20     
         40        ASSIGN                                                   !2, $20
   29    41        INIT_FCALL                                               'microtime'
         42        SEND_VAL                                                 <true>
         43        DO_ICALL                                         $22     
         44        ASSIGN                                                   !1, $22
   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                                         $26     
         54        SUB                                              ~27     $26, !1
         55        SEND_VAL                                                 ~27
         56        SEND_VAL                                                 5
         57        DO_ICALL                                         $28     
         58        CONCAT                                           ~29     $28, '+seconds'
         59        CONCAT                                           ~30     ~29, '%0A'
         60        ECHO                                                     ~30
   34    61        ECHO                                                     'Export+and+re-import+DOM%E2%80%A6+'
   35    62        INIT_FCALL_BY_NAME                                       'generate_random_dom'
         63        DO_FCALL                                      0  $31     
         64        ASSIGN                                                   !3, $31
   36    65        INIT_FCALL                                               'microtime'
         66        SEND_VAL                                                 <true>
         67        DO_ICALL                                         $33     
         68        ASSIGN                                                   !1, $33
   37    69        NEW                                              $35     'DOMDocument'
         70        SEND_VAL_EX                                              '1.0'
         71        SEND_VAL_EX                                              'UTF-8'
         72        DO_FCALL                                      0          
         73        ASSIGN                                                   !4, $35
   38    74        INIT_METHOD_CALL                                         !4, 'loadXML'
         75        INIT_METHOD_CALL                                         !3, 'saveXML'
         76        DO_FCALL                                      0  $38     
         77        SEND_VAR_NO_REF_EX                                       $38
         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                                         $41     
         86        SUB                                              ~42     $41, !1
         87        SEND_VAL                                                 ~42
         88        SEND_VAL                                                 5
         89        DO_ICALL                                         $43     
         90        CONCAT                                           ~44     $43, '+seconds'
         91        CONCAT                                           ~45     ~44, '%0A'
         92        ECHO                                                     ~45
   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.0.0


preferences:
147.8 ms | 1417 KiB | 23 Q