Why is “link” faster than “//link” in XPath?

北战南征 提交于 2021-01-28 07:35:47

问题


Given this XML,

library(xml2)

text = paste0(
  '<?xml version="1.0" encoding="UTF-8"?><items>',
  paste(rep(
  '<item type="greeting" id="9273938">
     <link type="1" id="139" value="Hi"/>
     <link type="1" id="142" value="Hello"/>
   </item>', 100),
        collapse = "\n"),
  '</items>')

x = xml_children(read_xml(text))

I can select all the link nodes by using "link" or "//link" and get the same result – but with very different speed:

bench::mark(
     link  = xml_find_all(x,   "link"),
  `//link` = xml_find_all(x, "//link"))[1:5]

# A tibble: 2 x 5
  expression      min   median `itr/sec` mem_alloc
  <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>
1 link          1.5ms   1.56ms     606.     10.6KB
2 //link       27.1ms  55.74ms      15.2   558.2KB

Why is there such a big difference?


回答1:


Because "link" only has to check if the current node has a child element named link, whereas "//link" has to check all elements in the document to see which are named link.

XPath notes:

  • "link" only checks the immediate children of the current node because the default axis is the child:: axis.
  • "//link" checks all elements in the document because // is a shortcut for /descendant-or-self::node()/, so "//link" is short for /descendant-or-self::node()/child::link.


来源:https://stackoverflow.com/questions/63853182/why-is-link-faster-than-link-in-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!