额外的空间会减慢处理器的速度吗?

发布于 2024-09-19 11:39:58 字数 497 浏览 3 评论 0原文

在学习如何“正确”取消设置节点后,我注意到使用 PHP 的 unset() 函数会留下制表符和空格。所以现在我有时在节点之间有一大块空白。我想知道 PHP 是否会遍历空格/返回符/制表符以及它是否最终会减慢系统速度。

我还想问是否有一个容易去除未设置留下的空间?

谢谢, Ryan

添加注释:

这就是我在取消设置节点后删除空格的方法,它对我有用。

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($xmlPath);
$dom->save($xmlPath);

After learning how to "correctly" unset a node, I noticed that using PHP's unset() function leaves the tabs and spaces behind. So now I have this big chunk of white space in between nodes at times. I'm wondering if PHP iterates through blank spaces/returns/tabs and whether it would eventually slow down the system.

I'm also asking whether there's an easy to remove the space unset leaves behind?

Thanks,
Ryan

ADDED NOTE:

This is how I removed the whitespaces after unsetting a node and it worked for me.

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($xmlPath);
$dom->save($xmlPath);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趁微风不噪 2024-09-26 11:39:58

它是否会减慢进程:可能根本不需要关心。

simpleXML 就是这么简单。如果您需要“漂亮”的输出,DOM 是您的朋友:

<?php
$xml = '
<xml>
        <node>foo </node>
        <other>bar</other>
</xml>';
$x = new SimpleXMLElement($xml);
unset($x->other);
echo $x->asXML();

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);
$dom->documentElement->removeChild($dom->documentElement->lastChild);
echo $dom->saveXML();

Wether it slows down the process: probably to little to care about.

And simpleXML is just that, simple. If you require a 'pretty' output, DOM is your friend:

<?php
$xml = '
<xml>
        <node>foo </node>
        <other>bar</other>
</xml>';
$x = new SimpleXMLElement($xml);
unset($x->other);
echo $x->asXML();

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);
$dom->documentElement->removeChild($dom->documentElement->lastChild);
echo $dom->saveXML();
无力看清 2024-09-26 11:39:58

XML 中的空白是 TextNodes,例如

<foo>
    <bar>baz</bar>
</foo>

<foo><- whitespace node
    -><bar>baz</bar><- whitespace node
-></foo>

如果删除 节点,您会得到

<foo><- whitespace node
    -><- whitespace node
-></foo>

我认为 SimpleXml 不会允许您轻松访问 Text 节点(可能通过 XPath),但 DOM 可以。有关详细信息,请参阅 Wrikken 的回答。现在您知道空白是一个节点,您还可以想象将其解析为节点会占用一些 cpu 周期。但是,我想说速度影响可以忽略不计。如有疑问,请使用一些真实世界的数据进行基准测试。


编辑:证明空白确实是

$xml = <<< XML
<foo>
    <bar>baz</bar>
</foo>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
foreach($dom->documentElement->childNodes as $node) {
    var_dump($node);
}

节点

object(DOMText)#4 (0) {}
object(DOMElement)#6 (0) {}
object(DOMText)#4 (0) {}

Whitespace in XML is TextNodes, e.g.

<foo>
    <bar>baz</bar>
</foo>

is really

<foo><- whitespace node
    -><bar>baz</bar><- whitespace node
-></foo>

If you remove the <bar> node, you get

<foo><- whitespace node
    -><- whitespace node
-></foo>

I think SimpleXml wont allow you to access the Text nodes easily (maybe via XPath) but DOM does. See Wrikken's answer for details. Now that you know that whitespace is a node, you can also imagine that parsing it into a node takes up some cpu cycles. However, I'd say the speed impact is negliglible. When in doubt, do a benchmark with some real world data.


EDIT: Proof that whitespace is really nodes

$xml = <<< XML
<foo>
    <bar>baz</bar>
</foo>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
foreach($dom->documentElement->childNodes as $node) {
    var_dump($node);
}

gives

object(DOMText)#4 (0) {}
object(DOMElement)#6 (0) {}
object(DOMText)#4 (0) {}
牵你的手,一向走下去 2024-09-26 11:39:58

实际上是 Libxml 进行 XML 解析,解析器读取空格与输入流中的所有其他字符相同(或文件)。大多数 PHP xml API 在底层都使用 Libxml(XmlReader、XmlWriter、SimpleXml Xslt、Dom...) - 其中一些允许您访问空白(例如 Dom、XmlReader),有些则不允许(例如 SimpleXML)

It's actually Libxml that does the XML parsing, whitespace is read by the parser the same as every other character in the input stream (or file). Most of the PHP xml APIs use Libxml under the hood (XmlReader, XmlWriter, SimpleXml Xslt, Dom...) - some of them give you access to whitespace (e.g. Dom, XmlReader), some don't (e.g. SimpleXML)

帥小哥 2024-09-26 11:39:58

快速回答所提出的问题:

我想知道 PHP 是否迭代
空格/回车/制表符以及是否
它最终会减慢
系统。

不,PHP(或 libxml)并没有真正迭代它。 理论上拥有更多的空白会降低系统的速度,尽管它很小以至于无法直接测量。您可以通过从 XML 中删除所有空白来自行测试。它不会让它变得更快。

我也想问有没有
轻松去除未设置的叶子空间
落后?

恐怕这并不容易。您可以将 SimpleXML 内容导入到 DOM 中,并使用 formatOutput 来完全重塑空白,如另一个答案中所建议的那样,或者您可以使用第三方库来为您完成此操作,但您不会找到一种简单的内置方法来做到这一点。

Quick answers to the questions asked:

I'm wondering if PHP iterates through
blank spaces/returns/tabs and whether
it would eventually slow down the
system.

No, PHP (or libxml) doesn't really iterate over it. Having more whitespace theorically slows down the system, although it's so small it can't be measured directly. You could test that by yourself by removing all whitespace from your XML. It wouldn't make it faster.

I'm also asking whether there's an
easy to remove the space unset leaves
behind?

No easy way I'm afraid. You can import your SimpleXML stuff to DOM and use formatOutput to completely remodel the whitespace, as suggested in another answer, or you can use a third party library that will do it for you, but you won't find an easy, built-in way to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文