Through the Interface: Microbenchmarking C# code

May 2015

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


« Getting AutoCAD's current directory from a .NET application | Main | Launching AutoCAD with a specific profile using .NET »

December 19, 2007

Microbenchmarking C# code

Jeremy Tammik, from our DevTech EMEA team, pointed me to this useful and interesting site:

It introduces a very easy way to benchmark functions in your application by simply tagging them with a [Benchmark] attribute (you also need to have included the C# file posted on the above site in your project, of course).

Jeremy also highlighted a very pertinent paragraph in the above site:

Use local variables where possible.

The CLR can do a more optimisations on code which doesn't (for the most part) "escape" from just local variables. For instance, it doesn't need to worry about other threads tampering with the variables. That's the reason the second example copies the number of iterations into a local variable before running the loop, and copies the result out of a local variable into a class variable right at the very end. This may or may not make a significant difference to your test (on the current CLR), but I believe it's good practice anyway - although you need to bear this in mind when considering using the results in a real application!

That's it for this post - I'll be back on Friday with my last post before the New Year (assuming I manage to stay away from my computer during our annual, end-of-year office shut-down).


TrackBack URL for this entry:

Listed below are links to weblogs that reference Microbenchmarking C# code:

blog comments powered by Disqus


10 Random Posts