Calculating average mode without creating too much garbage...

Started by
3 comments, last by Kylotan 11 years, 1 month ago

Hey everyone, I'm currently working on some behaviours for non playable characters in a game, and I need to find a reliable and fast method for finding the mode specific average of a set of values. This is not really a mathematics specific question rather a performance once, see the code must be per-formant in various situations where creating things like hash tables or anything else that creates orphan garbage to an extent that has potential to slow the game down to a crawl.

So far, I've found 4 different approaches to the issue as follows:


static int ModeA(int[] items)
{
    var groups = items.GroupBy(t => t);
    int maxCount = groups.Max(g => g.Count());
    return groups.First(g => g.Count() == maxCount).Key;
}
 
static int ModeB(IEnumerable<int> items)
{
    int maxVal = 0, maxCount = 0, curCount;
    var iter = items.GetEnumerator();
    while (iter.MoveNext())
    {
        curCount = items.Count(t => t == iter.Current);
        if (curCount > maxCount)
        {
            maxVal = iter.Current;
            maxCount = curCount;
        }
    }
    return maxVal;
}
 
static int ModeC(int[] items)
{
    int maxVal, maxCount, curCount;
    for (int i = maxVal = curCount = maxCount = 0; i < items.Length; i++)
    {
        var curVal = items[i];
        if ((curCount = items.Count(t => t == curVal)) > maxCount)
        {
            maxVal = curVal;
            maxCount = curCount;
        }
    }
    return maxVal;
}
 
static int ModeD(int[] items)
{
    int maxVal = 0, maxCount = 0, curCount = 0, curVal = 0, i = 0, j = 0;
    for (i = 0, j = 0; i < items.Length; i++, curCount = 0)
    {
        curVal = items[i]; 
 
        for (j = 0; j < items.Length; j++)
            if (items[j] == curVal) 
                curCount++;
 
        if (curCount > maxCount)
        {
            maxVal = curVal;
            maxCount = curCount;
        }
    }
    return maxVal;
}

And here is what I get performance wise if I ask each function to return the mode for 10,000 elements (timed with the Stopwatch class):

Method A Took: 00:00:00.0112893.
Method B Took: 00:00:02.9916290.
Method C Took: 00:00:02.3635998.
Method D Took: 00:00:00.5677500.
Hopefully you'll already be able to see my problem, method A is obviously the fastest but method D is more favourable (at least from what I can see as it doesn't create an enumerable collection like A and B do). In the real-time scenario I'll only be throwing a maximum of 300 elements at this method, which gives me something more reasonable:
Method A Took: 00:00:00.0087793.
Method B Took: 00:00:00.0039755.
Method C Took: 00:00:00.0026175.
Method D Took: 00:00:00.0007191.
And if I repeat this test within a loop a few times, the results change slightly:

Method A Took: 00:00:00.0000429.
Method B Took: 00:00:00.0027317.
Method C Took: 00:00:00.0021401.
Method D Took: 00:00:00.0005269.
Again obviously method A is the fastest, but in this 2rd test only after iteration. So my question is am I being too obsessed over this when I could just go with method A and sod GC, or is method D truly better even though I never see that much improvement after iteration and on larger arrays it struggles.
Thanks everyone for any feedback on the subject.
Aimee

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

Advertisement

OK maybe I am definately looking too far into this lol, I've found another method that's even faster than method A, except it does what I was trying to avoid by using a dictionary as follows:


static Dictionary<int, int> table = new Dictionary<int, int>();

static int ModeE(int[] items)
{
    table.Clear();
    for (int i = 0; i < items.Length; i++)
    {
        var item = items[i];
        if (!table.ContainsKey(item)) table.Add(item, 0);
        table[item]++;
    }
    return table.First(t => t.Value == table.Max(u => u.Value)).Key;
}

Does anyone see anything here that I'm missing that could cause unwanted garbage?

Aimee

We are now on Tumblr, so please come and check out our site!

http://xpod-games.com

You can pass a capacity value to the dictionary constructor, so that it doesn't have to allocate more memory as you add values into it. items.Length would be a reasonable value to use if you only use the dictionary once - otherwise, just pick a fairly large number.

But really, you don't need to allocate any memory for this. Sort the items, then iterate through them, looking for the longest consecutive run of identical values.
items.Sort(); // equal numbers are now contiguous in the array
int currTotal = 1;
int currentNumber = items[0];

// Initial state, having only considered one value
int modeValue = currentNumber;
int modeTotal = currTotal ;

// now look at the others
for (int i=1; i<items.Length; ++i)
{
    if (items[i] == items[i-1])
    {
        // One more of the same number
        currTotal++;

        // Biggest count so far? This will be our mode, until we find a bigger count.
        if (currTotal > modeTotal)
        {
            modeValue = currentNumber;
            modeTotal = currTotal;
        }
    }
    else
    {
        // This is a new number.
        // Only found one of them so far
        currTotal = 1;        
        currentNumber = items[i];
        // Never need to change the modeValue here as we always have an existing value with a count of at least 1
    }
}
There might be a bug or two in there as it's untested code, but hopefully you get the idea. The sorting operation means you only ever have to remember 2 values at a time, keeping hold of the largest, and since 2 is a constant you don't need any dynamic storage.

Do any of those functions work correctly? If there's a tie for the most popular value don't you have to take the mean of the tied values?

EDIT: Apparently not, although that makes the data multimodal... and you probably want to report all the modes of a data set in that case...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
You're thinking of the median. Taking the mean of each candidate for the mode would be meaningless since you're not necessarily working with interval or ratio data.

This topic is closed to new replies.

Advertisement