Reputation: 40032
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
Upvotes: 1
Views: 763
Reputation: 7474
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.
Upvotes: 1
Reputation: 1214
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);
Upvotes: 0
Reputation: 110071
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:
Upvotes: 0