static void Main(string[] args) { // 事实上指的是最近一次访问的键,而非访问最频繁的。 var sr = new StreamReader(File.OpenRead("tale.txt")); var data = sr.ReadToEnd().Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); var repeatTimes = 1; Console.WriteLine("BinarySearchST"); Console.WriteLine("Origin\tCached\tRatio"); long bstTimes = 0, bstcTimes = 0; var bst = new BinarySearchST <string, int>(); var bstc = new BinarySearchSTCached <string, int>(); bstTimes += SearchCompare.Time(bst, data); bstcTimes += SearchCompare.Time(bstc, data); Console.WriteLine(bstTimes / repeatTimes + "\t" + bstcTimes / repeatTimes + "\t" + (double)bstTimes / bstcTimes); Console.WriteLine("SequentialSearchST"); Console.WriteLine("Origin\tCached\tRatio"); long sstTimes = 0, sstcTimes = 0; var sst = new SequentialSearchST <string, int>(); var sstc = new SequentialSearchSTCached <string, int>(); sstTimes += SearchCompare.Time(sst, data); sstcTimes += SearchCompare.Time(sstc, data); Console.WriteLine(sstTimes + "\t" + sstcTimes + "\t" + (double)sstTimes / sstcTimes); }
static void Main(string[] args) { var repeatTimes = 20; var multiplyBy2 = 5; var n = 2000; for (var i = 0; i < multiplyBy2; i++) { Console.WriteLine("n=" + n); Console.WriteLine("Binary\tInterp\tRatio"); long bstTimes = 0, istTimes = 0; for (var j = 0; j < repeatTimes; j++) { var bst = new BinarySearchST <double, int>(); var ist = new InterpolationSearchST(); var data = SearchCompare.GetRandomArrayDouble(n); bstTimes += SearchCompare.Time(bst, data); istTimes += SearchCompare.Time(ist, data); } Console.WriteLine(bstTimes / repeatTimes + "\t" + istTimes / repeatTimes + "\t" + (double)bstTimes / istTimes); n *= 2; } }
static void Main(string[] args) { var n = 1000; var multiplyBy10 = 4; for (var i = 0; i < multiplyBy10; i++) { Console.WriteLine("n=" + n); // 构造表 var bst = new BinarySearchST <string, int>(n); var mst = new MoveToFrontArrayST <string, int>(n); var keys = SearchCompare.GetRandomArrayString(n, 3, 20); for (var j = 0; j < n; j++) { bst.Put(keys[j], j); mst.Put(keys[j], j); } // 构造查询 Array.Sort(keys); var querys = new string[10 * n]; int queryIndex = 0, keyIndex = 0; while (queryIndex < querys.Length) { var searchTimes = (int)Math.Ceiling((Math.Pow(0.5, keyIndex + 1) * querys.Length)); for (var j = 0; j < searchTimes && queryIndex < querys.Length; j++) { querys[queryIndex++] = keys[keyIndex]; } keyIndex++; } Shuffle(querys); var sw = new Stopwatch(); // 测试 MoveToFrontArrayST sw.Start(); for (var j = 0; j < querys.Length; j++) { mst.Get(querys[j]); } sw.Stop(); Console.WriteLine("MoveToFrontArrayST: " + sw.ElapsedMilliseconds); // 测试 BinarySearchST sw.Restart(); for (var j = 0; j < querys.Length; j++) { bst.Get(querys[j]); } sw.Stop(); Console.WriteLine("BinarySearchST: " + sw.ElapsedMilliseconds); n *= 10; } }
static void Test(IST <string, int>[] sts, int n) { var sw = new Stopwatch(); var data = SearchCompare.GetRandomArrayString(n, 3, 10); var item1 = "item1"; Array.Sort(data); // 有序的数组 Console.Write("Sorted Array: "); sw.Start(); for (var i = 0; i < n; i++) { sts[0].Put(data[i], i); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); // 逆序的数组 Console.Write("Sorted Array Reversed: "); sw.Restart(); for (var i = n - 1; i >= 0; i--) { sts[1].Put(data[i], i); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); // 只有一种键 Console.Write("One Distinct Key: "); sw.Restart(); for (var i = 0; i < n; i++) { sts[2].Put(item1, i); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); // 只有两种值 Console.Write("Two Distinct Values: "); sw.Restart(); for (var i = 0; i < n; i++) { sts[3].Put(data[i], i % 2); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }
static void Main(string[] args) { var n = 1000; var multiplyBy2 = 5; var repeatTime = 3; var averageHit = 10; for (var i = 0; i < multiplyBy2; i++) { Console.WriteLine("n = " + n); long sumSequential = 0, sumBinary = 0; for (var j = 0; j < repeatTime; j++) { sumBinary += SearchCompare.Performance(new BinarySearchST <string, int>(), n, averageHit); sumSequential += SearchCompare.Performance(new SequentialSearchST <string, int>(), n, averageHit); } Console.WriteLine("BinarySearchST: " + sumBinary / repeatTime); Console.WriteLine("SequentialSearchST: " + sumSequential / repeatTime); n *= 2; } }
static void Main(string[] args) { var n = 1000000; var m = 10; var addBy10 = 3; for (var i = 0; i < addBy10; i++) { var bst = new BinarySearchSTAnalysis <long, int>(n); var data = SearchCompare.GetRandomArrayLong(n, (long)Math.Pow(2, m), (long)Math.Pow(2, m + 1)); FrequencyCounter.MostFrequentlyKey(bst, data); Console.WriteLine("m=" + m + "\t" + bst.GetTimer.ElapsedMilliseconds + "\t" + bst.PutTimer.ElapsedMilliseconds + "\t" + bst.PutTimer.ElapsedMilliseconds / (double)bst.GetTimer.ElapsedMilliseconds); m += 10; } var st = new BinarySearchSTAnalysis <string, int>(); FrequencyCounter.MostFrequentlyWord("tale.txt", 0, st); Console.WriteLine("tales\t" + st.GetTimer.ElapsedMilliseconds + "\t" + st.PutTimer.ElapsedMilliseconds + "\t" + st.PutTimer.ElapsedMilliseconds / (double)st.GetTimer.ElapsedMilliseconds); Console.ReadLine(); }
static void Main(string[] args) { var n = 1000; var multiplyBy10 = 4; // 调和级数 var harmonicNumber = new double[n * (int)Math.Pow(10, multiplyBy10)]; harmonicNumber[0] = 1; for (var i = 1; i < harmonicNumber.Length; i++) { harmonicNumber[i] = harmonicNumber[i - 1] + 1 / (i + 1); } for (var i = 0; i < multiplyBy10; i++) { Console.WriteLine("n=" + n); // 构造表 var mst1 = new MoveToFrontArrayST <string, int>(n); var mst2 = new MoveToFrontArrayST <string, int>(n); var keys = SearchCompare.GetRandomArrayString(n, 3, 20); for (var j = 0; j < n; j++) { mst1.Put(keys[j], j); mst2.Put(keys[j], j); } // 构造查询 Array.Sort(keys); var queryZipf = new string[10 * n]; int queryIndex = 0, keyIndex = 0; while (queryIndex < queryZipf.Length) { var searchTimes = (int)Math.Ceiling(queryZipf.Length / (harmonicNumber[keyIndex + 1] * (i + 1))); for (var j = 0; j < searchTimes && queryIndex < queryZipf.Length; j++) { queryZipf[queryIndex++] = keys[keyIndex]; } keyIndex++; } var querys = new string[10 * n]; queryIndex = 0; keyIndex = 0; while (queryIndex < querys.Length) { var searchTimes = (int)Math.Ceiling((Math.Pow(0.5, keyIndex + 1) * querys.Length)); for (var j = 0; j < searchTimes && queryIndex < querys.Length; j++) { querys[queryIndex++] = keys[keyIndex]; } keyIndex++; } // Shuffle(querys); var sw = new Stopwatch(); // 测试 3.1.33 的序列 sw.Start(); for (var j = 0; j < queryZipf.Length; j++) { mst1.Get(querys[j]); } sw.Stop(); Console.WriteLine("3.1.33 Best: " + sw.ElapsedMilliseconds); // 测试 Zipf sw.Restart(); for (var j = 0; j < queryZipf.Length; j++) { mst2.Get(queryZipf[j]); } sw.Stop(); Console.WriteLine("Zipf: " + sw.ElapsedMilliseconds); n *= 10; } }