1

I have some code in C#, I am not very good in C# so used loops inside loops but they are taking too long. Is there any way to write code for very fast execution to save time with accuracy?

Here is the code.

    foreach (var vmain in vendorMainResult)
    {
        foreach (var povendor in potoDateOrders)
        {
            if (vmain.VendorNumber == povendor.VendorNumber && vmain.Year == povendor.Year)
            {
                vmain.ToDateOrders = povendor.ToDateOrders;
                vmain.OutstandingComm = povendor.OutstandingComm;
                break;
            }
        }
    }

Think about if there are 20,000 records in each collection then 20k x 20k takes 1-2 minute.

11
  • If your potoDateOrders list was ordered on vendor number and year, you wouldn't have to iterate over so many times. Commented Oct 15, 2015 at 15:40
  • According to the documentation, it is possible to run foreach on multiple thread. It can give you better perf on your operations. Commented Oct 15, 2015 at 15:42
  • 2
    @XavierWOLFF Rather than doing lots of superfluous work in parallel, it's way faster to just not do lots of superfluous work. Commented Oct 15, 2015 at 15:44
  • I would try and get a free profiler and actually measure where all the time is being taken up. If you don't profile you only have "guesses" on what is slow, if you do profile you will "know" what is slow. Commented Oct 15, 2015 at 15:46
  • @Servy Of course you are right. But we doesn't have enough informations about the context. So if he can't avoid superfluous work, it would at least be a little better to use Parralel class. Commented Oct 15, 2015 at 15:46

1 Answer 1

6

Use a Join to join two collections together efficiently.

var query = from vmain in vendorMainResult
    join povender in potoDateOrders
    on new 
    {
        vmain.VendorNumber, 
        vmain.Year,
    }
    equals new 
    {
        povendor.VendorNumber, 
        povendor.Year,
    }
    select new 
    {
        vmain,
        povendor,
    };
Sign up to request clarification or add additional context in comments.

3 Comments

Though this is of course a correct way to solve the problem, I think it's a good idea to note that the win comes with some cost. The original looped algorithm was n-squared in time, yes, but it was constant in space. This algorithm is much faster in time, but imposes a much higher burden to build the join and to allocate temporary objects. Trading off more space for less time is usually a good idea, and it is a good idea in this case, but I think we want to recognize that the tradeoff is in fact being made.
I have added a left join to complete my solution but uses this solution to start my foundation. Comparing to loops, this is much faster. I had four loops to fill in main object which were taking 2.30 minutes. After changing all in this shape, now it is taking less than 4 seconds. Thanks @Sevy and all to help.
Can you help me on this question:stackoverflow.com/questions/35332928/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.