由于 XPath 2.0/XSLT 2.0,我想将我的 XslCompiledTransform 迁移到 Saxon 9.7.0.6 HE,但它比 .NET 慢得多。
我使用默认副本 ident XSLT 和 15.000 个 xml 文件测试了每个版本:
Saxon with Parallel.ForEach: 00:05:02.9013605
XslCompiledTransform with Parallel.ForEach: 00:00:15.6724146
Saxon with foreach: 00:10:09.7763861
XslCompiledTransform with foreach: 00:03:00.3483324
我希望我做错了什么,XslCompiledTransform:
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsl);
XmlWriterSettings writerSettings = xslt.OutputSettings.Clone();
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.DtdProcessing = DtdProcessing.Ignore;
readerSettings.XmlResolver = null;
Parallel.ForEach(files, file =>
{
string target = Path.Combine(output, Path.GetFileName(file));
using (XmlReader xr = XmlReader.Create(file, readerSettings))
using (XmlWriter xw = XmlWriter.Create(target, writerSettings))
xslt.Transform(xr, xw);
});
撒克逊版本:
Processor processor = new Processor();
DocumentBuilder docBuilder = processor.NewDocumentBuilder();
docBuilder.DtdValidation = false;
docBuilder.SchemaValidationMode = SchemaValidationMode.None;
docBuilder.WhitespacePolicy = WhitespacePolicy.PreserveAll;
XsltCompiler compiler = processor.NewXsltCompiler();
XsltExecutable executable = compiler.Compile(new Uri(xsl));
Parallel.ForEach(files, file =>
{
string target = Path.Combine(output, Path.GetFileName(file));
XsltTransformer transformer = executable.Load();
XdmNode input = docBuilder.Build(new Uri(file));
transformer.InitialContextNode = input;
Serializer serializer = new Serializer();
serializer.SetOutputFile(target);
transformer.Run(serializer);
});
更新
我在没有 Visual Studio 调试的情况下进行了另一项测试,结果好多了:
Saxon: 00:00:41.5990128
XslCompiledTransform: 00:00:19.0441044
所以主要的减速是调试器本身,但仅限于撒克逊人。现在它只需要 .NET 版本的两倍时间,它不是超级棒,但我想我可以接受。
我能做些什么来让撒克逊人更快吗?也许玩代码或使用EE而不是HE?
这里有一些详细的基准信息,主要的性能问题是 DocumentBuilder.Build 方法。但即使是转换本身也比 .NET 版本慢两倍多:
撒克逊人:
。网: