views:

180

answers:

6

I have a C# app in which I am testing 3 ways to do something with XML.

I read a string of XML get a value out and then add it to a custom collection.

I am testing XMLDocument, XDocument and String manipulation to find the node value, in that order recording a start and end time for each.

However if I say mix the order up I get different speeds reported. Also when I press the button again to perform the test again the one that was slowest initially sometimes takes the least on subsequent tests.

I'm sure its all related to JIT and Garbage collection but what is the truest way to determine what runs the fastest.

I have downloaded EQATEC profiler but I'm lost on what to do with it.

Can someone advise me the best way to determine what methods run the quickest.

Thanks

A: 

Try using a profiling tool like ANTS from Red-Gate or dotTrace from JetBrains. The profilers will show you what percentage of time is spent in each method allowing you to identify which is the faster executed method.

Kane
It has to be a free one I'm afraid.
Jon
You could always use the trial version?
Kane
+1  A: 

Another profiler that might be worth trying is Slimtune.

Pike65
+1  A: 

Isn't it sufficient to each method with a set of sample input while measuring the time with StopWatch?

Philippe
Thats what I have done essentially however if I change the processing execution around ie/ TestString, TestXdoc, TestXML the time taken is diiferent if I run TestXML, TestXDoc, TestString Why is that?
Jon
have you discarded the first result?
Simon
I would guess that the first method in the slowest? You should probably run separate tests, like 1000 times TestXML with samples files, then calculate the average. Repeat with TestXDoc, then with TestString and compare results.
Philippe
A: 

Learn to use the tools you have:

http://www.eqatec.com/tools/profiler/guide

If you're getting inconsistent results, do the scientific thing and make control cases:

  • Either test multiple times in a loop so the first case is averaged out among the rest of the runs
  • or just test the first case (restarting the app each time).
David B
A: 

You can use Timing class from "Data structures and algorithms using c#" by Michael McMillian.

Roughly it'll be as follows:

TimeSpan startingTime, duration;
GC.Collect();
GC.WaitForPendingFinalizers();
startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
DoLengthyWork();
duration = Process.GetCurrentProcess.Threads(0).UserProcessorTime.Subtract(startingTime);
Bolek Tekielski
+1  A: 

Jon Skeet has written a pretty good bechmarking 'framework' for measuring simple operations like this that I think would be pretty well suited to checking each of those methods, which you could find here: http://www.yoda.arachsys.com/csharp/benchmark.html (note, this is just timing the operations, not profiling the heap usage or anything like that).

An important point to keep in mind (Jon notes it on that site above, and Eric Lippert is fond of mentioning it whenever the topic comes up) is that when you run code multiple times, it only gets JIT'd the first time, so if you want to measure the true differences for each method, measure at least twice and compare the second runs - I would imagine this is the result of your shifting orders.

Matt Enright