.net - Benchmarking small code samples in C#, can this implementation be improved? -


On the SO often I benchmark myself a small part of the code to see which duplication is the fastest

Often I get comments that the benchmarking code does not take into consideration the jooting or garbage collector.

I have the following simple benchmarking functions that I have gradually developed:

  Fixed zero profiles (string description, intertion, action fanc) {// warm Up fenc (); // clean gc Compile (); Var watch = new stopwatch (); Watch.Start (); For (int i = 0; i  

Usage:

  Profile ("A description", how_many_iterations_to_run, () = & gt; {// ... the code has been profiled });  

Is there any flaw in this implementation? Is it great to show that Implementation X implementation is higher than Z graph more than Y? Can you think in any way that you will improve it?

Edit It is very clear that time-based approach (in opposition to iterations), has been liked, does anyone have any implementation where impact on time checks? Do not you?

There is a modified function here: as recommended by the community, to modify it in a community wiki do not hesitate .

Process.GetCurrentProcess () running on the top priority to reduce fluctuation due to the constant double profile (string description, interation, action throws) {// other processes / threads . PriorityClass = ProcessPriorityClass.High; Thread Existing content Priority = Thread priority. The most; // warmc func (); Var watch = new stopwatch (); // clean gc Compile (); GC.WaitForPendingFinalizers (); GC.Collect (); Watch.Start (); For (int i = 0; i

Make sure that you compile it in release with enabled optimization, and run a test outside Visual Studio this last part is important because JIT will make its optimization debugger Is connected to, even in release mode.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -