Пример #1
0
        public void TryFindSubStringSuccess(string key, int pos, string expectedKey, string expectedValue)
        {
            var cache = new StringMap <string>();

            cache.Add("abcde", "1");
            cache.Add("abc", "2");
            cache.Add("abcd", "3");
            cache.Add("bar", "4");
            cache.Add("mi", "5");
            cache.Add("mm", "6");
            var success = cache.TryGetBySubString(key, pos, out var actualKey, out var actual);

            Assert.AreEqual(true, success);
            Assert.AreEqual(expectedKey, actualKey);
            Assert.AreEqual(expectedValue, actual);

            success = cache.TryGetBySubString(key, pos, out actual);
            Assert.AreEqual(true, success);
            Assert.AreEqual(expectedValue, actual);
        }
Пример #2
0
        public void AddThenGetFail(string key, int pos)
        {
            var cache = new StringMap <string>();

            cache.Add("abc", "d");
            cache.Add("foo", "e");
            cache.Add("bar", "f");
            var success = cache.TryGetBySubString(key, pos, out var actual);

            Assert.AreEqual(false, success);
            Assert.AreEqual(null, actual);
            Assert.AreEqual(null, actual);
        }
Пример #3
0
        public void TryFindSubStringWorstCase()
        {
            // this is not a really meaningful comparison as Find does much more work
            // finding the end while substring gets the end for free.
            // interesting as a base line.
            var cache = new StringMap <string>();

            cache.Add("abc0", "d");
            cache.Add("abc01", "e");
            cache.Add("abc1", "e");
            cache.Add("abc11", "e");
            cache.Add("abc2", "f");
            cache.Add("abc21", "f");

            const string text0 = "   abc0   ";
            const string text1 = "   abc1   ";
            const string text2 = "   abc2   ";
            string       cached;

            cache.TryGetBySubString(text0, 4, out cached);

            var subString = text0.Substring(3, 4);

            var sw = Stopwatch.StartNew();
            var n  = 1000000;

            for (int i = 0; i < n; i++)
            {
                switch (i % 3)
                {
                case 0:
                    cache.TryGetBySubString(text0, 4, out cached);
                    continue;

                case 1:
                    cache.TryGetBySubString(text1, 4, out cached);
                    continue;

                case 2:
                    cache.TryGetBySubString(text2, 4, out cached);
                    continue;

                default:
                    throw new Exception();
                }
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| cache.TryFind(text, 4, out cached) {n:N0} times with cache took: {sw.ElapsedMilliseconds} ms");

            sw.Restart();
            for (int i = 0; i < n; i++)
            {
                switch (i % 3)
                {
                case 0:
                    subString = text0.Substring(3, 4);
                    continue;

                case 1:
                    subString = text1.Substring(3, 4);
                    continue;

                case 2:
                    subString = text2.Substring(3, 4);
                    continue;

                default:
                    throw new Exception();
                }
            }

            sw.Stop();
            Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| text.Substring(3, 4)               {n:N0} times            took: {sw.ElapsedMilliseconds} ms");
        }