1

I'm have an issue with high performance inside a loop. I'm not sure how I can optimise the code to be honest. With the Visual Studio Performance Explorer I've managed to fix a few bugs I've had, but now the problem seems to be due to the number of loops this particular for loop is doing.

Here's a screenshot from Visual Studio:

Visual Studio Performance Screenshot

I'm guessing that since the int i = 0 is being marked the performance issue is because of a large number of loops. Alsp, I know that ExecuteReader is marked as high performance but I don't think I can change that, I mean it's the requests I have from my database.

EDIT

Code:

public IQueryable<Match> GetMatches()
{
    DLTxOdds dlTxOdds = new DLTxOdds();
    IQueryable<Match> matches = dlTxOdds.GetMatches();
    List<Match> mList = new List<Match>();

    // For each retrieved match check whether it contains any sure bets
    foreach (Match m in matches)
    {
        List<SureBet> matchSureBets = new List<SureBet>();

        // Get sport of match
        m.sport = dlTxOdds.GetMatchSport(m.sportid);
        // Get orders of match, ordered by the odd type id
        m.matchOffers = dlTxOdds.GetMatchOffers(m.matchid).OrderBy(o => o.ot).ToList();

        int tempID = -1;
        List<Offer> tempMatchOffersList = new List<Offer>();
        // For every match offer get its respective bookmaker, odds and odd type
        for (int i = 0; i < m.matchOffers.Count; i++)
        {
            m.matchOffers[i].bookmaker = dlTxOdds.GetBookmaker(m.matchOffers[i].bid);
            m.matchOffers[i].odds = dlTxOdds.GetOfferOdds(m.matchOffers[i].offer_id);
            m.matchOffers[i].oddType = dlTxOdds.GetOddType(m.matchOffers[i].ot);

            // Filter the match offers so that offers with the same odd type id are checked for sure bets
            tempID = m.matchOffers[i].ot;
            // if current index is last one we cannot check for the next element therefore add to list and check for sure bets
            if (i + 1 != m.matchOffers.Count)
            {
                // if next odd type in list is different, check for sure bets
                if (m.matchOffers[i + 1].ot != tempID)
                {
                    tempMatchOffersList.Add(m.matchOffers[i]);
                    // Get sure bets
                    matchSureBets.AddRange(GetSureBets(tempMatchOffersList));
                    tempMatchOffersList = new List<Offer>();
                }
                // else add to temporary list
                else
                    tempMatchOffersList.Add(m.matchOffers[i]);
            }
            else
            {
                tempMatchOffersList.Add(m.matchOffers[i]);
                // Get sure bets
                matchSureBets.AddRange(GetSureBets(tempMatchOffersList));
            }
        }

        if (m.matchOffers.Count() != 0)
        {
            // if match has any sure bets add to list...
            if (matchSureBets.Count() != 0)
            {
                m.sureBets = matchSureBets.Where(s => s.max == true).OrderByDescending(s => s.profit).ToList();
                mList.Add(m);
            }
        }
    }

    mList = mList.OrderByDescending(m => m.sureBets[0].profit).ToList();
    return mList.AsQueryable<Match>();
}
Daniel Grima
  • 2,765
  • 7
  • 34
  • 58
  • 1
    Dont post images but code. – Tim Schmelter Aug 28 '13 at 15:47
  • It is not too difficult to copy/paste the relevant code. In this way it is impossible to understand what's happening – Steve Aug 28 '13 at 15:47
  • Yes you're right I'm sorry. I've posted the code now. Thanks for your replies. – Daniel Grima Aug 28 '13 at 15:56
  • *Who Knows* what it means when it marks that `int i=0`in red? I would just take some [*stackshots*](http://stackoverflow.com/a/378024/23771). If you're trying to make it take less time, you might be able to perform fewer iterations. You might also see that the functions being called inside the loop are doing stuff that could be optimized. It's doing `Add` and `AddRange` that, if they're on most of the stack samples, might be done another way, saving a large fraction of time (for a healthy speedup). – Mike Dunlavey Aug 28 '13 at 16:14
  • You get that because you use the profiler in sampling mode instead of instrumentation mode. Of course a loop takes more time, it executes a lot more instructions. The most important thing you need to know when you profile code is to know when you are *done*. – Hans Passant Aug 28 '13 at 16:24

0 Answers0