❔ Optimization thoughts on a loop.

Looking for potential ways to speed up this operation. Any thoughts?
The entries of each HashSet will change regularly and this is iterated frequently.
    public class MyClass { }
    //This will aways have 4 entries.
    private List<HashSet<MyClass>> _regions = new List<HashSet<MyClass>>();
    private void Test()
    {

        for (int i = 0; i < 2000; i++)
        {
            CheckIndex(0);
            if (i % 2 == 0)
                CheckIndex(1);
            if (i % 3 == 0)
                CheckIndex(2);
            if (i % 4 == 0)
                CheckIndex(3);
        }

        //Logic
        void CheckIndex(int index)
        {
            HashSet<MyClass> classes = _regions[index];
            //do things to classes.
        }
    }
Was this page helpful?