码迷,mamicode.com
首页 > 其他好文 > 详细

C# 小规模查找集合性能测试

时间:2014-08-11 17:10:42      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   数据   for   ar   art   cti   

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

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

 

bubuko.com,布布扣

 

代码:

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);
        }


    }
}

 

C# 小规模查找集合性能测试,布布扣,bubuko.com

C# 小规模查找集合性能测试

标签:blog   os   io   数据   for   ar   art   cti   

原文地址:http://www.cnblogs.com/a_bu/p/3904855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!