Example #1
0
        //same as before, but for lists instead of arrays
        int FindOptimalBreakpointList()
        {
            int optimalBreakpoint = 2;

            var random = new System.Random();

            Stopwatch stopwatchLinear = new Stopwatch();
            Stopwatch stopwatchBinary = new Stopwatch();

            float lin = 0f;
            float log = 1f;

            // continue increasing "optimalBreakpoint" until linear becomes slower than log
            // result is around 15-16, varies a bit due to random nature of test
            while (lin <= log)
            {
                int numOfDiffArrays = 100;
                int numOfTestPerArr = 10000;

                // u = uniform grid, r = uniform random
                float u, r;
                ///Linear Search
                stopwatchLinear.Stop();
                stopwatchLinear.Reset();

                List <float> items = RandomMath.IdentityList(optimalBreakpoint);

                float selectedItem; //simulate selecting from array

                List <float> list = new List <float>(optimalBreakpoint);

                for (int i = 0; i < optimalBreakpoint; i++)
                {
                    list.Add(0f);
                }

                for (int k = 0; k < numOfDiffArrays; k++)
                {
                    RandomMath.RandomWeightsList(ref list, random);
                    RandomMath.BuildCumulativeDistribution(list);

                    stopwatchLinear.Start();
                    for (int i = 0; i < numOfTestPerArr; i++)
                    {
                        u            = i / (numOfTestPerArr - 1f);
                        selectedItem = items[list.SelectIndexLinearSearch(u)];

                        r            = (float)random.NextDouble();
                        selectedItem = items[list.SelectIndexLinearSearch(r)];
                    }

                    stopwatchLinear.Stop();
                }

                lin = stopwatchLinear.ElapsedMilliseconds;

                /// Binary Search
                stopwatchBinary.Stop();
                stopwatchBinary.Reset();

                for (int k = 0; k < numOfDiffArrays; k++)
                {
                    RandomMath.RandomWeightsList(ref list, random);
                    RandomMath.BuildCumulativeDistribution(list);

                    stopwatchBinary.Start();
                    for (int i = 0; i < numOfTestPerArr; i++)
                    {
                        u            = i / (numOfTestPerArr - 1f);
                        selectedItem = items[list.SelectIndexBinarySearch(u)];

                        r            = (float)random.NextDouble();
                        selectedItem = items[list.SelectIndexBinarySearch(r)];
                    }
                    stopwatchBinary.Stop();
                }

                log = stopwatchBinary.ElapsedMilliseconds;

                optimalBreakpoint++;
            }

            return(optimalBreakpoint);
        }