標籤: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); } }}