Array Contains Performance

后端 未结 4 1448
遇见更好的自我
遇见更好的自我 2021-01-24 18:32

I have a program which works with arrays and at some point I have to check if a lsit of values are inside an array, the function that does this takes about 70% of the program CP

4条回答
  •  臣服心动
    2021-01-24 18:47

    I found a method, thanks to @sous2817 comment, that works perfectly for me.

    public static int[] GetRandomNumbers(int count, Random RNG)
        {
            HashSet randomNumbers = new HashSet();
            for (int i = 0; i < count; i++)
                while (!randomNumbers.Add(RNG.Next(count))) ;
            return randomNumbers.ToArray();
        }
    

    This works ~10 times faster then what I was doing and works nicely with the rest of my program.I needed the code to be "linear" so other solutions mess my program. Anyway, thanks to everyone that helped :)

提交回复
热议问题