コード例 #1
0
        public static int BestTaskCount(int arrayCount, int minTaskCount, int maxTaskCount)
        {
            int[] buffer = Utils.GenerateIntArray(arrayCount, 15);

            long elapsed       = 0;
            long minElapsed    = long.MaxValue;
            int  bestTaskCount = -1;

            for (int i = minTaskCount; i <= maxTaskCount; i++)
            {
                Stopwatch watch = Stopwatch.StartNew();
                //-------------------------
                int s = ThreadArray.Sum(buffer, i);
                //-------------------------
                watch.Stop();
                elapsed = watch.ElapsedMilliseconds;
                Console.WriteLine($"TaskCount = {i}: Sum of int[{arrayCount}] = {s}, elapsed time is {elapsed} ms.");
                if (elapsed < minElapsed)
                {
                    minElapsed    = elapsed;
                    bestTaskCount = i;
                }
            }
            return(bestTaskCount);
        }
コード例 #2
0
        public void Sum_CompareThreadAndUsualSumOnAllProc_Equal()
        {
            int[] arr       = Utils.GenerateIntArray(100000, 29);
            int   sampleSum = ThreadArray.Sum(arr, 0, arr.Length);
            int   testedSum = ThreadArray.Sum(arr, Environment.ProcessorCount);

            Assert.Equal(sampleSum, testedSum);
        }
コード例 #3
0
        public void Sum_CompareThreadAndUsualSumOn3Proc_Equal()
        {
            int[] arr       = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int   sampleSum = ThreadArray.Sum(arr, 0, arr.Length);
            int   testedsum = ThreadArray.Sum(arr, 3);

            Assert.Equal(sampleSum, testedsum);
        }