How to define "parsing time" in an XSLT transform using C # code?

I wrote C # code to start the conversion of XML to XML (XSLT). Since the conversion depends on the size of the XML, and my input XML file can vary from Kilo-Bytes to Mega-Bytes, I want to define "the time taken to parse my input file and generate the output." I can display the "value" through the GUI or console, no problem. The goal is to store the " time in seconds or milliseconds " in a variable,

any links for reference or guidance regarding this idea will also be useful.

Does it depend on the system configuration?
I mean, in this case, the parsing time depends on the system and the system depending on the environment?
If so ... is it possible to make this a system independent code?
I look forward to hearing .. than Q ..

+3
source share
2 answers

You are missing a significant component of the total conversion time: the time it takes to compile the stylesheet itself .

Here's how to get this time:

// Create the XslCompiledTransform and load the stylesheet.
XslCompiledTransform xslt = new XslCompiledTransform();

Stopwatch watch = Stopwatch.StartNew();  
  xslt.Load("someXsl.xsl");
watch.Stop();   

TimeSpan xsltCompileTime = watch.Elapsed;

Note that the load / compile time of using XSLT stylesheets using is XslCompiledTransformusually very long compared to the time required to complete typical small conversions. This is why in a production system one could cache the loaded stylesheet and reuse it without load.

+2
source

I'm not sure I fully understand, but maybe just:

Stopwatch watch = Stopwatch.StartNew();
// where "xslt" is your prepared XslTransform or XslCompiledTransform
xslt.Transform(input, args, results); 
watch.Stop();
TimeSpan elapsed = watch.Elapsed; // how long

:

string seconds = elapsed.TotalSeconds.ToString("0.000");

:

Stopwatch watch = Stopwatch.StartNew();
XPathDocument sourceDoc = new XPathDocument(location);
watch.Stop();
TimeSpan parseTime = watch.Elapsed;
watch.Reset();

watch.Start();
xslt.Transform(sourceDoc, args, results);
watch.Stop();
TimeSpan transformTime = watch.Elapsed;
+5

Source: https://habr.com/ru/post/1723839/


All Articles