Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8859275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:01:07+00:00 2026-06-14T15:01:07+00:00

When testing the relative performance of different method implementations I find myself re-writing functions

  • 0

When testing the relative performance of different method implementations I find myself re-writing functions similar to this.

private static long Measure(
    int iterations,
    Func<string, string> func,
    string someParameter)
{
    var stopwatch = new Stopwatch();
    stopwatch.Start();
    for (var i = 0; i < iterations; i++)
    {
        func(someParameter);
    }

    stopwatch.Stop();
    return stopwatch.ElapsedTicks;
}

Rather than rewriting this function for every method singnature that I test, would it be possible to write a generic implementation for performance testing any delagate? Something along the lines of

    private static long Measure(
    int iterations,
    Delegate func,
    params object[] parameters)
{
    ...
}

or

    private static long Measure<TDelegate>(
    int iterations,
    TDelegate func,
    params object[] parameters)
{
    if (!typeof(TDelegate).IsSubclassOf(typeof(Delegate)))
    {
        throw new ArgumentException("Not a delegate", "func");
    }

    ...
}

If I can do this would it make sense to Compile an Expression<TDelegate> before performing the iterations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:01:09+00:00Added an answer on June 14, 2026 at 3:01 pm

    Why not use an Action and you can set the parameters in a lambda.

    private static long Measure(int iterations, Action action)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        for (var i = 0; i < iterations; i++)
        {
            action();
        }
    
        stopwatch.Stop();
        return stopwatch.ElapsedTicks;
    }
    

    Then you call it like

    Measure(100, () => MyMethod(arg1,arg2,...));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far, while testing the speeds of different approaches to writing the same function,
For testing purposes I want to use Jetty 8 to serve only static content.
I'm setting up a publish to a relative path for testing locally (especially xml
Update: TTF file seems to be there after testing. Update: changed to a relative
In a bit of Python I'm writing (a command line and filter testing tool:
I'm using links with relative paths within my phonegap application. I've been testing on
Say you have a link with a relative path that looks like this: <a
I have two jQuery functions that I've built and in testing them, I noticed
I'm a controls developer and a relative newbie to unit testing. Almost daily, I
I am in the process of writing a testing script that will simply run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.