在 XSLT 中寻求更好的性能:xsl:element name=“div”与相比
时,XSLT 的性能更高,
<xsl:element name="div">
<xsl:attribute name="class">someclass</xsl:attribute>
</xsl:element>
在编写 XHTML 元素或只是将其写出
<div class="someclass"></div>
它在处理速度/性能等方面有什么区别吗?
What has more performance in XSLT while writing an XHTML element
<xsl:element name="div">
<xsl:attribute name="class">someclass</xsl:attribute>
</xsl:element>
or just write it out
<div class="someclass"></div>
Does it make any difference in processing-speed / -performance etc. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑 XSLT 编译器可能会在内部将一种转换为另一种,果然,至少一些他们做:
更一般地说,这听起来像是一种微观优化,不太可能带来比选择更具可读性版本的好处更重要的改进。
I suspected that XSLT compilers probably convert one into the other internally and, sure enough, at least some of them do:
More generally, this smells like the kind of micro-optimization that's unlikely to render an improvement that outweighs the benefits of choosing the more readable version.
任何 XSL 转换器都会将两种变体映射到相同的内部表示中。刚刚使用 saxonb-xslt 9 测试了一百万次调用:绝对没有区别。
Next to any XSL transformer will map both variants into the same internal representation. Just tested a million calls with saxonb-xslt 9: Absolutely no difference.
可读性仅在开发周期中才重要。使用 XSL 动态呈现的高负载站点将希望缩短看似不可能的小时间。
要测试编译器中哪个速度更快,请创建两个 XSL,将相同的事情重复 10,000 次,然后在前端对处理速度进行基准测试。然后将时间差除以 10,000,即可得到真正的速度差。
根据 XSL 的大小也会产生影响。 xsl:element 中完全设计的页面将比直接的 HTML 大得多。但是,如果您在该方法中使用 XSL,则应该重新考虑将特定数据分解为 XML/XSL 包含内容,并且页面模板的其余部分在其他地方完成。
Readability is important only in the development cycle. High load sites that use dynamic presentation with XSL will want to shave off seemingly impossible small times.
To test which is faster in your compiler, create two XSL that repeat the same thing 10,000 times and then benchmark the processing speed on the front end. Then divide time difference by 10,000 and you get your true difference in speed.
Depending on the size of the XSL will also have an impact. A fully designed page in xsl:element will be so much larger than straight HTML. However if you use XSL in that method, you should rethink breaking down specific data into XML/XSL includes and the rest of the page templating is done somewhere else.
写出的元素是否具有更好的性能取决于 XSLT 处理器。处理器可能会优化第一个元素,使其具有与第二个元素相同的性能。实现了 XSLT 处理器后,我建议将元素写出而不是使用 xsl:element 可能会同样快或更快,并且不太可能变慢。
Whether writing out the element has better performance depends on the XSLT processor. The processor may optimize the first to have the same performance as the second. Having implemented an XSLT processor, I'd advise that writing the element out instead of using xsl:element is likely to be either as fast or faster, and unlikely to be slower.
您不会从这种重构中获得任何明显的性能提升(您能感觉到一微秒的差异吗)?
但是,我强烈建议使用最具可读性的版本:
即使在元素具有动态计算值的属性的情况下,始终尝试编写:
而不是:
You are not going to obtain any sensible performance gain from such refactoring (can you feel a microsecond's difference)?
However, I strongly recommend to use the most readable version:
Even in the case when the element has attributes whose value is dynamically calculated, always try to write:
instead of: