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

  • Home
  • SEARCH
  • 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 992133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:13:01+00:00 2026-05-16T06:13:01+00:00

I have a class that modifies data via some extension methods. In order to

  • 0

I have a class that modifies data via some extension methods. In order to debug performance, I have created some rough debug code to make multiple calls to the same methods using the same data a number of times. I am finding that it consistently takes a significantly longer time to do the calculations the first time through the loop than subsequent calls.

For example, for a small set of data, it appears to take about 5 seconds to do the calculations, while each subsequent call is a second or so.

Thanks,
wTs

The code looks something like this:

Test Code

void TestCode()
{
    for (int i = 0; i < iterationsPerLoop; i++)
    {
        DateTime startTime = DateTime.Now;

        // The test is actually being done in a BackgroundWorker
        dispatcher.Invoke(DispatcherPriority.Normal,
                            (Action)(() => this.PropertyCausingCodeToRun = "Run";
        while (this.WaitForSomeCondition)
            Thread.Sleep(125);
        DateTime endTime = DateTime.Now;

        double result = endTime.Subtract(startTime).TotalSeconds;
    }
}

Method where extension methods called

private static List<ObservableItem> GetAvailableItems(MyObject myObject)
{
    var items = new List<ObservableItem>(myObject.Items.ToList());
    var selectedItems = items.OrderByDescending(item => item.ASortableProperty)
                             .SetItemIsAvailable(false)
                             .SetItemPriority() 
                             .OrderByDescending(item => item.Priority) 
                             .Where(item => item.Priority > 0) 
                             .SetItemIsAvailable(true) 
                             .OrderBy(item => item.Date);

    return selectedItems.ToList();
}

Extension Methods (ObservableItems all created on different thread)

static class MyExtensionMethods
{
    public static IEnumerable<T> SetItemIsAvailable<T>(this IEnumerable<T> sourceList,
            Boolean isAvailable) where T : ObservableItem
    {
        Action<T> setAvailable = i => i.IsAvailable = isAvailable;

        List<DispatcherOperation> invokeResults = new List<DispatcherOperation>();

        foreach (var item in sourceList)
        {
            invokeResults.Add(
                item.ItemDispatcher.BeginInvoke(setAvailable , new object[] { item }));
        }

        invokeResults.ForEach(ir => ir.Wait());
        return sourceList;
    }

    public static IEnumerable<T> SetItemPriority<T>(this IEnumerable<T> sourceList) where T : ObservableItem
    {
        Action<T, double> setPriority = new Action<T, double>((item, priority) =>
            {
                item.Priority = priority;
            });

        List<DispatcherOperation> invokeResults = new List<DispatcherOperation>();

        foreach (var item in sourceList)
        {
            double priority = ......;  // Some set of calculations

            invokeResults.Add(
                item.ItemDispatcher.BeginInvoke(setPriority, 
                            new object[] { asset, priority }));
        }

        invokeResults.ForEach(ir => ir.Wait());
        return sourceList;
    }
}
  • 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-05-16T06:13:02+00:00Added an answer on May 16, 2026 at 6:13 am

    Most often, the first time methods are called, there is some overhead associated with the JIT compilation time. This will have an effect (though most likely not that much).

    However, looking at your code, you’re spending a huge amount of time waiting on asynchronous calls marshalling to the UI via the dispatcher. This is going to put a large hit on your overall performance, and slow this way down.

    I’d recommend doing all of your operations in one dispatch call, and using Invoke instead of BeginInvoke. Instead of marshalling one message per item, just marshal a single delegate that includes the foreach loop through your items.

    This will be significantly faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't see why this would be bad. I think… May 17, 2026 at 1:57 am
  • Editorial Team
    Editorial Team added an answer One good place to go looking for things you need… May 17, 2026 at 1:57 am
  • Editorial Team
    Editorial Team added an answer I'm the guy at NetQuarry that worked up the MasterPage-derived… May 17, 2026 at 1:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have some code in a reusable class that modifies some types. Here's a
Imagine I have a class used to represent some trivial numerical data, like a
I have been building a core data application to administrate some data and I've
Just a simple question: I have read that a class should be made static
I have a two part question Best-Practice I have an algorithm that performs some
Let's say I have an Image class and I want to provide some operations
Working with the MVVM pattern, I have a pair of view model classes that
I have a class which get all the bytes from a file, then it
I noticed (and verified in the sunspot code) the following behavior class Foo <
This doesn't have to be Java, but it's what I'm dealing with. Also, not

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.