Hashset vs Array, is further optimization possible?
<p>I have a code that works like the following all throughout my program. One example would be pathfinding, where I create snapshots of a dynamic environment and use the created map as basis for the aStar-algorithm.</p>

<p>As the execution time of the code impacts the maximum amount of units I can have utilizing the pathfinding, I'd like to optimize here as much as possible.</p>

<p>The (I believe) performance optimal solution would be this:</p>

<pre><code>  static int runtest(bool[,] coll, int size, int globalSize)
    {
        int counter = 0;
        for (int x = -size; x &lt; size; x++)
        {
            for (int y = -size; y &lt; size; y++)
            {
                if (coll[x+ globalSize, y+ globalSize])
                {
                    counter++;

                    int tx = x;
                    int ty = y;
                }
            }
        }
        return counter;
    }
</code></pre>

<p>However I'm not happy with the amount of wasted memory when using arrays, so I am currently using this solution:</p>

<pre><code>static int runtest(HashSet&lt;int&gt; coll, int size)
    {
        int counter = 0;
        for (int x = -size; x &lt; size; x++)
        {
            for (int y = -size; y &lt; size; y++)
            {
                if (coll.Contains(calculateHash(x, y)))
                {
                    counter++;

                    int tx = x;
                    int ty = y;
                }
            }
        }
        return counter;
    }

 public static int calculateHash(float x1, float y1)
        {
            int x = (int)Math.Floor(x1);
            int y = (int)Math.Floor(y1);

            return calculateHash(x, y);
        }

      //[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static int calculateHash(int x, int y) {
            return x + (y &lt;&lt; 16);
        }
    }
</code></pre>

<p>The Array implementation is about 5 times faster, is this something I have to live with or can I somehow optimize the hash-version further?</p>

<p>Full Test Code:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int size = 240;
            HashSet&lt;Vector2&gt; VecHash = new HashSet&lt;Vector2&gt;();
            HashSet&lt;int&gt; intHash = new HashSet&lt;int&gt;();
            bool[,] boolArr = new bool[size * 2, size * 2];


        for (int x = -size; x &lt; size; x++) {
            for (int y = -size; y &lt; size; y++) {
                VecHash.Add(new Vector2(x, y));
                intHash.Add(calculateHash(x,y));
                boolArr[x+size, y + size] = true;
            }
        }


        Console.WriteLine("Press enter to start");
        Console.ReadLine();

        int reps = 1000;
        int testSize = 180;
        Stopwatch sw = new Stopwatch();
        while (true) {
            long counter = 0;
            sw.Start();
            for (int i = 0; i &lt; reps; i++)
            {
                counter += runtest(VecHash, testSize);
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks + "\tVecHash items: \t" + counter);
            counter = 0;
            sw.Restart();
            for (int i = 0; i &lt; reps; i++)
            {
                counter += runtest(intHash, testSize);
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks + "\tIntHash items: \t" + counter);
            counter = 0;
            sw.Restart();
            for (int i = 0; i &lt; reps; i++)
            {
                counter += runtest(boolArr, testSize, size);
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks + "\tboolArr items: \t" + counter);
            sw.Reset();
            Console.ReadLine();
        }

    }


    static int runtest(HashSet&lt;Vector2&gt; coll, int size)
    {
        int counter = 0;

        var allrelevant = coll.Where(a =&gt; a.x &gt;= -size &amp;&amp; a.x &lt; size &amp;&amp; a.y &gt;= -size &amp;&amp; a.y &lt; size);
        foreach (var item in allrelevant) {
            counter++;

            float tx = item.x;
            float ty = item.y;
        }

        return counter;
    }
    static int runtest(HashSet&lt;int&gt; coll, int size)
    {
        int counter = 0;
        for (int x = -size; x &lt; size; x++)
        {
            for (int y = -size; y &lt; size; y++)
            {
                if (coll.Contains(calculateHash(x, y)))
                {
                    counter++;

                    int tx = x;
                    int ty = y;
                }
            }
        }
        return counter;
    }
    static int runtest(bool[,] coll, int size, int globalSize)
    {
        int counter = 0;
        for (int x = -size; x &lt; size; x++)
        {
            for (int y = -size; y &lt; size; y++)
            {
                if (coll[x+ globalSize, y+ globalSize])
                {
                    counter++;

                    int tx = x;
                    int ty = y;
                }
            }
        }
        return counter;
    }

    public static int calculateHash(float x1, float y1)
    {
        int x = (int)Math.Floor(x1);
        int y = (int)Math.Floor(y1);

        return calculateHash(x, y);
    }

  //[MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static int calculateHash(int x, int y) {
        return x + (y &lt;&lt; 16);
    }
}

public struct Vector2
{
    public readonly float x;
    public readonly float y;

    public Vector2(float x, float y)
    {
        this.x = x;
        this.y = y;
    }
}
</code></pre>

<p>}</p>