How to detect the "parsing time" in XSLT transformation using C# code?
I have written a C# code for triggering an XML to XML (XSLT) transformation. As the transformation depends upon the size of the XML and my Input XML file can vary from Kilo-Bytes to Mega-Bytes, I want to detect the "time taken to parse my input file and generate output" .. I can display the "value" via GUI or console, no problem about it. The aim is to save the "time in seconds or milliseconds" in a variable
any links for reference or tutorial concerning to this idea would be helpful too .. Does it depend on the system configuration? I mean is it the case tha开发者_StackOverflowt .. the parsing time varies from system to system according to the environment? If yes.. then, Is it possible to make it a system independent code? eagerly waiting for reply .. thanQ ..I'm not sure I understand fully, but perhaps simply:
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
If you want the elapsed time in seconds and milliseconds:
string seconds = elapsed.TotalSeconds.ToString("0.000");
If you want separate timings for parse vs transform:
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;
You are missing a significant component of the total time for a transformation: 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;
Do note that the time to Load/compile an XSLT stylesheet with XslCompiledTransform
is typically very big compared to the time required to run typical small transformations. This is why, in a production system one would consider caching a loaded stylesheet and reusing it without load ever after.
精彩评论