Kean Walmsley

July 2009

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 31  

Twitter Updates

    follow me on Twitter



    « 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:

    http://www.yoda.arachsys.com/csharp/benchmark.html

    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

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200e54faac4d18833

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

    Comments

    That looks like an excellent site! Well done to Jeremy for finding it and Kean for blogging it!

    Seasonal Greetings to all at DevTech, have a good shutdown and New Year period :)

    Nice "mini" profiler.

    For more comprehensive execution profiling, and assuming one doesn't have pockets deep enough for a high-end solution like like Rational Purify, have a look at this:

    http://www.softprodigy.com/profilesharp/index.aspx

    Someone else in my team also suggested looking at the "Visual Studio Performance Tool profiler". I haven't investigated any of these tools personally.

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search