项目中包含浮点运算,大概每秒 20 - 100 万左右. 其计算结果每秒只包含1000个左右。 因此大量运算是重复性的。程序运行时,cpu 在 3% - 10% 浮动。打算将结果缓存。根据键值索值。

目前考虑数据类型有: SortedList , SortedDictionary , Dictionary , List 。测试结果如下:

 

 

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static System.Random random = new Random();
        static void Main(string[] args)
        {
            System.Threading.Thread.Sleep(1000);

            System.Drawing.PointF[] p = new System.Drawing.PointF[1024];
            for (int i = 0; i < 1024; i++)
            {
                p[i] = new System.Drawing.PointF((float)random.NextDouble(), (float)random.NextDouble());
            }

            double[] find = new double[10];
            find[0] = p[10].X;
            find[1] = p[800].X;
            find[2] = p[200].X;
            find[3] = p[199].X;
            find[4] = p[485].X;
            find[5] = p[874].X;
            find[6] = p[912].X;
            find[7] = p[110].X;
            find[8] = p[12].X;
            find[9] = p[600].X;


            testSortList(p, find);
            Console.WriteLine();
            testSortedDictionary(p, find);
            Console.WriteLine();
            testDictionary(p, find);
            Console.WriteLine();
            testList(p, find);

            Console.Read();

        }

        static void testSortList(System.Drawing.PointF [] p , double [] find) {
            SortedList<double, double> s = new SortedList<double, double>(1024);

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            foreach (System.Drawing.PointF sp in p)
            {
                s.Add(sp.X, sp.Y);
            }
            stopWatch.Stop();
            Console.WriteLine("SortedList add:" + stopWatch.ElapsedTicks);

            stopWatch.Reset();
            stopWatch.Start();
            foreach (double d in find)
            {
                Console.WriteLine(s.ContainsKey(d));
            }
            stopWatch.Stop();

            Console.WriteLine("SortedList find:" + stopWatch.ElapsedTicks);
        }

        static void testSortedDictionary(System.Drawing.PointF[] p, double[] find)
        {
            SortedDictionary<double, double> s = new SortedDictionary<double, double>();

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            foreach (System.Drawing.PointF sp in p)
            {
                s.Add(sp.X, sp.Y);
            }
            stopWatch.Stop();
            Console.WriteLine("SortedDictionary add:" + stopWatch.ElapsedTicks);

            stopWatch.Reset();
            stopWatch.Start();
            foreach (double d in find)
            {
                Console.WriteLine(s.ContainsKey(d));
            }
            stopWatch.Stop();

            Console.WriteLine("SortedDictionary find:" + stopWatch.ElapsedTicks);
        }

       
        static void testDictionary(System.Drawing.PointF[] p, double[] find)
        {
            Dictionary<double, double> s = new Dictionary<double, double>(1024);

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            foreach (System.Drawing.PointF sp in p)
            {
                s.Add(sp.X, sp.Y);
            }
            stopWatch.Stop();
            Console.WriteLine("Dictionary add:" + stopWatch.ElapsedTicks);

            stopWatch.Reset();
            stopWatch.Start();
            foreach (double d in find)
            {
                Console.WriteLine(s.ContainsKey(d));
            }
            stopWatch.Stop();

            Console.WriteLine("Dictionary find:" + stopWatch.ElapsedTicks);
        }


        static void testList(System.Drawing.PointF[] p, double[] find)
        {

            List<System.Drawing.PointF> lst = new List<System.Drawing.PointF>();

            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            foreach (System.Drawing.PointF sp in p)
            {
                lst.Add(sp);
            }
            stopWatch.Stop();
            Console.WriteLine("List add:" + stopWatch.ElapsedTicks);

            stopWatch.Reset();
            stopWatch.Start();
            System.Drawing.PointF p2;
            bool bln = false ;
            foreach (double d in find)
            {
                p2 = lst.Find(new Predicate<System.Drawing.PointF>((p1) =>
                {
                    return p1.X == d;
                }));     

                //foreach (System.Drawing.PointF p1 in lst)
                //{
                //    if (p1.X == d)
                //    {
                //        bln = true;
                //        break;
                //    }
                //}

                //for (int i = 0; i < lst.Count; i++ )
                //{
                //    if (lst[i].X == d)
                //    {
                //        bln = true;
                //        break;
                //    }
                //}

                if (bln)
                {
                    Console.WriteLine("True");
                    bln = false;
                }
                else {
                    Console.WriteLine("False");
                }
            }
            stopWatch.Stop();

            Console.WriteLine("List find:" + stopWatch.ElapsedTicks);
        }


    }
}

 

 posted on 2014-08-11 15:35  仰光  阅读(819)  评论(0编辑  收藏  举报