If you need a value from a dictionary and you aren't sure if it is in the dictionary you should use TryGetValue()
because it is faster. See also my answer here : Message bus in C# Message bus in C#
If you need a value from a dictionary and you aren't sure if it is in the dictionary you should use TryGetValue()
because it is faster. See also my answer here : Message bus in C#
If you need a value from a dictionary and you aren't sure if it is in the dictionary you should use TryGetValue()
because it is faster. See also my answer here : Message bus in C#
Another point to improve would be this
public ProfileData GetChild(string groupName)
{
lock (Children)
{
if (!Children.ContainsKey(groupName))
{
ProfileData child = new ProfileData(groupName, cleanInterval);
child.parent = this;
Children.Add(groupName, child);
}
return Children[groupName];
}
}
If you need a value from a dictionary and you aren't sure if it is in the dictionary you should use TryGetValue()
because it is faster. See also my answer here : Message bus in C#
That beeing said, changing the former method to
public ProfileData GetChild(string groupName)
{
lock (Children)
{
ProfileData child;
if(Children.TryGetValue(groupName, out child))
{
return child;
}
child = new ProfileData(groupName, cleanInterval);
child.parent = this;
Children.Add(groupName, child);
return child;
}
}
should improve the speed.
Another point to improve would be this
public ProfileData GetChild(string groupName)
{
lock (Children)
{
if (!Children.ContainsKey(groupName))
{
ProfileData child = new ProfileData(groupName, cleanInterval);
child.parent = this;
Children.Add(groupName, child);
}
return Children[groupName];
}
}
If you need a value from a dictionary and you aren't sure if it is in the dictionary you should use TryGetValue()
because it is faster. See also my answer here : Message bus in C#
That beeing said, changing the former method to
public ProfileData GetChild(string groupName)
{
lock (Children)
{
ProfileData child;
if(Children.TryGetValue(groupName, out child))
{
return child;
}
child = new ProfileData(groupName, cleanInterval);
child.parent = this;
Children.Add(groupName, child);
return child;
}
}
should improve the speed.
Having not done much with Parallel.ForEach
I nevertheless would like to suggest to change it like so
Parallel.ForEach(queue, (profilingRequest) =>
{
var currentTime = now - profilingRequest.timestamp;
for (int i = 0; i < intervals.Length; i++)
{
if (currentTime < intervals[i])
{
var currentStat = stats[i];
if (currentStat.queueCount == 0)
{
currentStat.minDuration = currentStat.maxDuration = profilingRequest.duration;
}
else if (profilingRequest.duration < currentStat.minDuration)
{
currentStat.minDuration = profilingRequest.duration;
}
else if (profilingRequest.duration > currentStat.maxDuration)
{
currentStat.maxDuration = profilingRequest.duration;
}
currentStat.queueCount++;
totalDurations[i] += profilingRequest.duration;
if (profilingRequest.containsExceptions)
{
currentStat.exceptionsCount++;
}
}
}
});
By calculating the currentTime
outside of the loop the access of the profilingRequest
is limizted to one time and it isn't calculated for each iteration of the loop.
By using var currentStat = stats[i];
(if the compiler doesn't do this internally) the stats
item doesen't need to be accessed by its index that often.
I wonder what should happen if profilingRequest.duration == currentStat.maxDuration
this seems to be a forgotten edge case.
Like you see I have added braces although they might be optional. They won't slow down the execution but your code gets better readable and less error prone.