The only reason I'm posting this is because I actually believe that my code perfomance is being affected by this code block where I use a foreach loop inside another one.
I was wondering if anyone would suggest anything different that could help the performance or perhaps point out some other flaws in the code.
public override void DisplayScore()
{
byte MessageLocation = 0;
foreach (var kvp in PlayerScores.OrderByDescending((s => s.Value)))
{
if (MessageLocation == 5)
break;
if (MessageLocation == PlayerScores.Count)
break;
foreach (var player in PlayerList.Values)
{
SendMessage(MessageLocation, "My text");
}
Score++;
}
}
As you can see, it's just displaying the top 5 scores (in different locations) from a dictionary in a descending order and sending them to a list of players from another dictionary.
MessageLocationand where you are updating its value?foreach,breaketc. will not cause performance problems until you have millions of records. Try to profile it, or at least try to debug it. Most probably, the problem is somewhere inSendMessage.SendMessagedoes. Please post a minimal reproducible example which demonstrates the problem.