Spiga

最新评论

Re:位图求交集 yuejianjun 2012-04-28 15:05  
位运算排序 对1000000数据排序耗时30毫秒 最上面的数组排序,相应耗时37毫秒
Re:位图求交集 yuejianjun 2012-04-28 14:53  
位运算排序 /// <summary> /// 原理:32 int 在二进制中有32位,每一位为0或1,当为1时,表示该位置存在 /// </summary> class BitMap { private static int WORD = 32; private static int SHIFT = 5;//移动5个位,左移则相当于乘以32,右移相当于除以32取整 private static int MASK = 0x1F;//16进制下的31 public static int N = 1000000; private static int[] a = new int[1 + N / WORD]; //置位函数——用"|"操作符,i&MASK相当于mod操作 //m mod n 运算,当n = 2的X次幂的时候,m mod n = m&(n-1) //设置第i位, 用"|"操作符 public static void set(int i) { a[i >> SHIFT] |= (1 << (i & MASK)); } //清除第i位, 用"&~"操作符 public static void clear(int i) { a[i >> SHIFT] &= ~(1 << (i & MASK)); } /// <summary> /// 获取第i位, 用"&"操作符 /// </summary> /// <param name="i"></param> /// <returns>当返回值大于0时,这该值是存在的</returns> public static int test(int i) { return a[i >> SHIFT] & (1 << (i & MASK)); } } class Program { private static int WORD = 32; private static int SHIFT = 5;//移动5个位,左移则相当于乘以32,右移相当于除以32取整 private static int MASK = 0x1F;//16进制下的31 public static int N = 10000000; static void Main(string[] args) { int i; for (i = 0; i < BitMap.N; i++) { //BitMap.clear(i);//初始化位图 } int[] aa = { 2, 5, 4, 7, 8, 3,14 ,43929,123,56778,123456}; for (i = 0; i < aa.Length; i++) { BitMap.set(aa[i]); } for (i = 0; i < BitMap.N; i++) { //Console.WriteLine(BitMap.test(i)); if (BitMap.test(i) > 0) { Console.WriteLine(i + " " + BitMap.test(i)); } } Console.Read(); } }
Re:位图求交集 yuejianjun 2012-04-27 18:08  
/// <summary> /// 性能优化。minLengthInts要排好序,其为要求求交集中的最短数组 。性能最好,10万数据,平均4毫秒;100万数据,平均45毫秒。性能比上面提高20% /// </summary> /// <param name="list">除minLengthInts外,其它求交集的数组</param> /// <param name="minLengthInts">要求排序,并且是数组中,长度最短的一个</param> /// <param name="MaxNumber"></param> /// <returns></returns> public static List<int> BitArray(List<List<int>> list, int[] minLengthInts, int MaxNumber = int .MaxValue) { List<int> temp = new List<int>(); int[] DataForStore = new int[MaxNumber]; for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list[i].Count; j++) { DataForStore[list[i][j]]++; } } int count=list.Count ; for (int i = 0; i < minLengthInts.Length; i++) { if (DataForStore[minLengthInts[i]] == count) { temp.Add(minLengthInts[i]); } } return temp; }
Re:位图求交集 yuejianjun 2012-04-26 23:00  
Test: static void Main(string[] args) { //BitMap bm = new BitMap(100, 10000); //bm.CreateRandomData();//产生随机数 //bm.Sort();//排序 //bm.PrintDataAfterSort();//输出 List<List<int>> list = new List<List<int>>(); List<int> temp = new List<int>(); for (int i = 70000; i < 1000000; i++) { temp.Add(i); } list.Add(temp); temp = new List<int>(); for (int i = 40000; i < 1000000; i++) { temp.Add(i); } list.Add(temp); temp = new List<int>(); for (int i = 1000; i < 100000; i++) { temp.Add(i); } list.Add(temp); for (int i = 0; i < 10; i++) { DateTime dt = DateTime.Now; temp = 位图求交集.BitList(list); DateTime dt1 = DateTime.Now; TimeSpan ts = dt1 - dt; int n = ts.Milliseconds; dt = DateTime.Now; temp = 位图求交集.BitList(list, 1000000); dt1 = DateTime.Now; ts = dt1 - dt; int m = ts.Milliseconds; Console.WriteLine("数据结果数: " + temp .Count + "优化处理:" + n + " 非优化处理:" + m); } foreach (int i in temp) { Console.WriteLine(i); } Console.ReadLine(); }
Re:ThreadPool 使用 yuejianjun 2011-10-27 18:06  
[code=csharp] using System; using System.Threading; class CalculateTest { static void Main() { Calculate calc = new Calculate(); Console.WriteLine("Result = {0}.", calc.Result(234).ToString()); Console.WriteLine("Result = {0}.", calc.Result(55).ToString()); Console.ReadKey(); } } class Calculate { //基数、数1、数2、数3 double baseNumber, firstTerm, secondTerm, thirdTerm; //自动重置同步事件数组 AutoResetEvent[] autoEvents; //手动重置同步事件 ManualResetEvent manualEvent; //随机数生成器 Random randomGenerator; public Calculate() { //初始化同步事件 autoEvents = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) }; manualEvent = new ManualResetEvent(false); } void CalculateBase(object stateInfo) { baseNumber = randomGenerator.NextDouble(); //置事件状态为终止状态,使等待线程继续 manualEvent.Set(); } void CalculateFirstTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); //等待手动重置事件终止 manualEvent.WaitOne(); firstTerm = preCalc * baseNumber * randomGenerator.NextDouble(); //置自动事件终止信号 autoEvents[0].Set(); } void CalculateSecondTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); //等待手动重置事件终止 manualEvent.WaitOne(); secondTerm = preCalc * baseNumber * randomGenerator.NextDouble(); //置自动事件终止信号 autoEvents[1].Set(); } void CalculateThirdTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); //等待手动重置事件终止 manualEvent.WaitOne(); thirdTerm = preCalc * baseNumber * randomGenerator.NextDouble(); //置自动事件终止信号 autoEvents[2].Set(); } public double Result(int seed) { randomGenerator = new Random(seed); //将待执行工作加入线程 ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateBase)); ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateFirstTerm)); ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateSecondTerm)); ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateThirdTerm)); //等待所有自动事件终止 WaitHandle.WaitAll(autoEvents); //重置手动事件为非终止状态 manualEvent.Reset(); return firstTerm + secondTerm + thirdTerm; } } [/code]
Re:ThreadPool 使用 yuejianjun 2011-10-27 18:06  
using System; using System.Threading; class CalculateTest { static void Main() { Calculate calc = new Calculate(); Console.WriteLine("Result = {0}.", calc.Result(234).ToString()); Console.WriteLine("Result = {0}.", calc.Result(55).ToString()); Console.ReadKey(); } } class Calculate { //基数、数1、数2、数3 double baseNumber, firstTerm, secondTerm, thirdTerm; //自动重置同步事件数组 AutoResetEvent[] autoEvents; //手动重置同步事件 ManualResetEvent manualEvent; //随机数生成器 Random randomGenerator; public Calculate() { //初始化同步事件 autoEvents = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false) }; manualEvent = new ManualResetEvent(false); } void CalculateBase(object stateInfo) { baseNumber = randomGenerator.NextDouble(); //置事件状态为终止状态,使等待线程继续 manualEvent.Set(); } void CalculateFirstTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); //等待手动重置事件终止 manualEvent.WaitOne(); firstTerm = preCalc * baseNumber * randomGenerator.NextDouble(); //置自动事件终止信号 autoEvents[0].Set(); } void CalculateSecondTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); //等待手动重置事件终止 manualEvent.WaitOne(); secondTerm = preCalc * baseNumber * randomGenerator.NextDouble(); //置自动事件终止信号 autoEvents[1].Set(); } void CalculateThirdTerm(object stateInfo) { double preCalc = randomGenerator.NextDouble(); //等待手动重置事件终止 manualEvent.WaitOne(); thirdTerm = preCalc * baseNumber * randomGenerator.NextDouble(); //置自动事件终止信号 autoEvents[2].Set(); } public double Result(int seed) { randomGenerator = new Random(seed); //将待执行工作加入线程 ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateBase)); ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateFirstTerm)); ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateSecondTerm)); ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateThirdTerm)); //等待所有自动事件终止 WaitHandle.WaitAll(autoEvents); //重置手动事件为非终止状态 manualEvent.Reset(); return firstTerm + secondTerm + thirdTerm; } }
Re:List排序的例子 yuejianjun 2011-09-22 16:13  
10 List<int> intList = new List<int>(); 11 intList.AddRange(new[] {9, 23, 2, 1, 2, 3}); 12 Console.WriteLine("List<int>"); 13 foreach (int i in intList) 14 { 15 Console.Write(i.ToString()+" "); 16 } 17 Console.WriteLine("\nUse List.sort"); 18 intList.Sort(); 19 foreach (int i in intList) 20 { 21 Console.Write(i.ToString() + " "); 22 }
Re:写日志 写SQL语句执行日志 路过秋天 2010-09-19 14:55  
代码很长有点乱,如果后面来个截图,也许是个亮点。