コード例 #1
0
    public void test_remove_random_bit_vs_hashset_performance()
    {
        HashSet <int> hashSet = new HashSet <int> {
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
            21, 22, 23, 24, 25, 26, 27, 28, 29, 30
        };
        int       bitmask   = 2147483647;
        Stopwatch stopWatch = new Stopwatch();

        int[] selection0 = new int[31];
        int[] selection1 = new int[31];


        // ---

        UnityEngine.Random.seed = 0;
        int c = 0;

        stopWatch.Start();
        while (bitmask != 0)
        {
            selection0[c++] = BitOpsHelper.RemoveRandomBit(ref bitmask);
        }
        stopWatch.Stop();
        var t0 = stopWatch.ElapsedTicks;

        UnityEngine.Random.seed = 0;
        c = 0;
        stopWatch.Start();
        while (hashSet.Count > 0)
        {
            int i = UnityEngine.Random.Range(0, hashSet.Count);
            var e = hashSet.GetEnumerator();
            while (i-- >= 0)
            {
                e.MoveNext();
            }
            hashSet.Remove(selection1[c++] = e.Current);
        }
        stopWatch.Stop();
        var t1 = stopWatch.ElapsedTicks;

        // ---

        //UnityEngine.Debug.Log("t0: " + t0);
        //UnityEngine.Debug.Log("t1: " + t1);
        //string str0 = "", str1 = "";
        //for (int i = 0; i < 31; i++)
        //{
        //    str0 += selection0[i] + ", ";
        //    str1 += selection1[i] + ", ";
        //}
        //UnityEngine.Debug.Log("selection0: " + str0);
        //UnityEngine.Debug.Log("selection1: " + str1);

        Assert.AreIdenticalSets(selection0, selection1, (a, b) => a == b);
        Assert.IsTrue(t0 < t1);
    }
コード例 #2
0
        public static int RemoveRandomBit(ref int bitmask)
        {
            var randomBitIndex = UnityEngine.Random.Range(0, PopCount(bitmask));
            var actualBitIndex = BitOpsHelper.BFind(bitmask, randomBitIndex + 1);

            bitmask &= ~(1 << actualBitIndex);
            return(actualBitIndex);
        }
コード例 #3
0
ファイル: BitOpsHelperTests.cs プロジェクト: r2d2m/roadgen
 public void test_bfind()
 {
     // 11 = 1011, so the third flipped bit is in index 3
     Assert.AreEqual(3, BitOpsHelper.BFind(11, 3));
     // 3 = 11, so there's no third flipped bit and BFind returns 32
     Assert.AreEqual(32, BitOpsHelper.BFind(3, 3));
     // searching for the 0th flipped bit always causes BFind to return 32
     Assert.AreEqual(32, BitOpsHelper.BFind(1, 0));
 }