C# 小規模尋找集合效能測試

來源:互聯網
上載者:User

標籤:blog   os   io   資料   for   ar   art   cti   

項目中包含浮點運算,大概每秒 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);        }    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.