コード例 #1
0
        private static int ParallelTaskLoop(List <string> names, RedBlackSearchTree strTree)
        {
            int countCPU = 4;

            Task <int>[] tasks = new Task <int> [countCPU];
            for (var j = 0; j < countCPU; j++)
            {
                tasks[j] = Task <int> .Factory.StartNew(
                    (object p) =>
                {
                    int count = 0;
                    for (int i = (int)p; i < names.Count; i += countCPU)
                    {
                        if (strTree.Contains(names[i]))
                        {
                            count++;
                        }
                    }
                    return(count);
                }, j);
            }
            int total = 0;

            for (var i = 0; i < countCPU; i++)
            {
                total += tasks[i].Result;
            }
            return(total);
        }
コード例 #2
0
        private static int SequentialLoop(List <string> names, RedBlackSearchTree strTree)
        {
            int count = 0;

            for (int i = 0; i < names.Count; i++)
            {
                if (strTree.Contains(names[i]))
                {
                    count++;
                }
            }
            return(count);
        }