tags:

views:

213

answers:

2

Hi all,
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 that .. 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 ..

Here is the solution and Complements:
Answer by marc-gravell is accurate ..
thank you very much marc and thank you "stackoverflow" :)

+5  A: 

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;
Marc Gravell
Doesn't work ..error saying:"Stopwatch" is not defined or not in current context .. :(
infant programmer
Add a `using System.Diagnostics;` to the top.
Marc Gravell
Or; click into the word Stopwatch and press [Ctrl]+[.] then [Ret]; or right click -> Resolve -> using {blah}
Marc Gravell
ya .. its working fine thanQ .. :)
infant programmer
Does it depend on the system configuration?I mean is it the case that .. 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 .
infant programmer
Assuming you still mean "time taken to parse my input file and generate output", then it will always depend on the local CPU and IO capabilities - but the same code will should regardless. Or do you want *two* numbers - one for parsing the input, one for the transform?
Marc Gravell
You have posted accurate answer .. thank you very much .. :)
infant programmer
+2  A: 

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.

Dimitre Novatchev
@Dimitre, Ya you are correct, but I have modified it in my practical implementation, I just needed an Idea of implementation .. hence I have accepted Marc .. In my code the timer starts before the XML, XSL files are loaded and stops after the transformation is done and o/p file is generated .. only one timer is all required and displays the transformation time (in milliseconds) in console window .. thanx for the concern anyway .. :-)
infant programmer