コード例 #1
0
        public static void Main(string[] args)
        {
            Dictionary<DateTime, int> dic = new Dictionary<DateTime, int> ();
            BinaryTree<DateTime, int> tree = new BinaryTree<DateTime, int> ();

            DateTime time = Convert.ToDateTime ("2012/7/30 4:30:00");
            for (int i = 0; i < 60000; i++) {
                if (!dic.ContainsKey(time)) {
                    dic.Add (time, i);
                    tree.Add (time, i);
                }
                time = time.AddSeconds (i);
            }

            var min = Convert.ToDateTime ("2012/7/30 4:30:00");
            var max = Convert.ToDateTime ("2012/7/30 4:40:00");

            var watch = Stopwatch.StartNew ();
            var result1 = dic.Keys.Where (i => i >= min && i <= max).Select (i => dic [i]).ToList ();
            watch.Stop ();
            Console.WriteLine("字典查找耗费时间:{0}ms,获取总数:{1}", watch.ElapsedMilliseconds, result1.Count);

            watch = Stopwatch.StartNew ();
            var result2 = tree.SearchRange(min, max);
            watch.Stop ();
            Console.WriteLine("二叉树耗费时间:{0}ms,获取总数:{1}", watch.ElapsedMilliseconds, result2.Count);
        }