Array Contains Performance

后端 未结 4 1389
遇见更好的自我
遇见更好的自我 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:45

    Instead of shuffle, you can generate the possible values in a list and take values at random positions. That way you can start using the items even before the whole row is generated:

        IEnumerable RandomRange(int count) {
            var random = new Random();
            var list = new int[count];
            for (int i = 0; i < count; i++) list[i] = i;
    
            while (count > 0) {
                var i = random.Next(count);
                yield return list[i] ;
                list[i] = list[--count];
            }
        }
    

    sample use:

    foreach(var item in RandomRange(10))
        // ...
    

提交回复
热议问题