问题
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 thechild::
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