Please navigate to the bottom of the page for Table of Contents

Sunday, July 29, 2012

How to time any function

One key aspect during performance testing is to time how long a function takes to execute. In this small post, I will show how to create a generic timing function that takes as argument the function to time and shows the time to execute along with the output of the function:

/// <summary>
/// Times the execution of a function and outputs both the elapsed time and the function's result.
/// </summary>
static void Time<T>(Func<T> work)
    var sw = Stopwatch.StartNew();
    var result = work();
    Console.WriteLine(sw.Elapsed + ": " + result);

To call the time, use the following invocation style:

Time(() => DoSomeWork());


  1. Splendid blog post. I might want to pass on thank you for moving and significant data and I like your purpose of investigation. Much obliged to you! I want to comprehend writing this kind of matter great and eye-getting data I procure from this website. Much obliged to you for posting such a fine bit of writing.

  2. This comment has been removed by the author.

  3. Thanks for the topic. I found useful information for myself. I want to share HandmadeWriting there is also a lot of useful information.

  4. The technical skills and expertise the team web designer in San Francisco has dedicated to the work has saved the client valuable time

  5. Expert project management and a solutions-oriented approach highlight their work.
    top creative agency

  6. We valued that their team continually challenged us to improve our approach.
    mobile app design service

  7. They're very process and detail-oriented in their approach, ensuring the quality and organization of the project phases are well laid out.
    top branding company

  8. This is one of the most important blogs that I have seen, keep it up!
    best logo company

  9. This blog is further than my expectations. Nice work guys!!!
    best logo design services

  10. Hello, this is fastidious post I actually loved reading this.
    creative web design agency

  11. I'm in no doubt coming back again to read these articles and blogs.
    top website designing companies

  12. Loved your blog page!!!The stuff that you have remarked up here is superbly wonderful and I vigorously thank you for the same...
    UI UX design agency