半月沒來博客園,換了一份新工做,開始繁瑣的事情一大堆,實在沒閒工夫寫什麼了,這篇大概是我半年前寫在別處的,不過我以爲是不錯的方法,至少我我的這麼以爲。回溯法是不可控的,有時候會超出咱們意料以外產生不妙的結果,最多見的也就是內存泄漏。一下是原文照搬過來的,不過是我本身寫的,我也不加出處了。(若是是引用別人的我會加上出處,咱不埋沒別人的智慧。)golang
回溯方法是很容易想到,又不容易想到的,每每,咱們思惟更容易進入的是回溯法。可是回溯法有着它的弊端,很是明顯的弊端是做用域內產生的變量和引用在回溯法調用未完成時,不能釋放(對於大部分編輯器來講,排除有着優化能力的編輯器)。若是咱們在某一方法中使用極多的回溯調用,在方法中不能及時的對方法做用域內的變量和引用釋放,最終會形成內存不足和cpu的計算負荷增大(內存機制中能夠將過剩的數據轉存到虛擬內存、硬盤,這個就不說了)。使用棧(隊)式的循環,能夠輕易避免回溯法,並且棧(隊)式的數據再使用以後能夠很方便的拋出移除。某些時候就是這樣,一個小小的改動,可讓一個程序在某種特定的環境中起死回生。(以前作過一個數獨運算器的算法,後來的優化改進就是爲了不回溯)算法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace 避免回溯方法 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string path = AppDomain.CurrentDomain.BaseDirectory; 14 15 List<string> fileList1 = new List<string>(); 16 FunctionHuishuo(path, ref fileList1); 17 18 List<string> fileList2 = new List<string>(); 19 FunctionQueue(path, ref fileList2); 20 21 List<string> fileList3 = new List<string>(); 22 FunctionStack(path, ref fileList3); 23 } 24 25 /// <summary> 26 /// 回溯法 27 /// </summary> 28 /// <param name="path"></param> 29 /// <param name="fileList"></param> 30 private static void FunctionHuishuo(string path, ref List<string> fileList) 31 { 32 if (true) 33 { 34 string[] files = null; 35 try 36 { 37 files = Directory.GetFiles(path); 38 } 39 catch { } 40 41 if (files != null && files.Length > 0) 42 { 43 fileList.AddRange(files); 44 } 45 } 46 47 if (true) 48 { 49 string[] folders = null; 50 try 51 { 52 folders = Directory.GetDirectories(path); 53 } 54 catch { } 55 56 if (folders != null && folders.Length > 0) 57 { 58 foreach (string folder in folders) 59 { 60 FunctionHuishuo(folder, ref fileList); 61 } 62 } 63 } 64 } 65 66 /// <summary> 67 /// 堆棧法 68 /// </summary> 69 /// <param name="path"></param> 70 private static void FunctionStack(string path, ref List<string> fileList) 71 { 72 Stack<string> stack = new Stack<string>(); 73 stack.Push(path); 74 75 while (stack.Count > 0) 76 { 77 string dir = stack.Pop(); 78 79 string[] files = null; 80 try 81 { 82 files = Directory.GetFiles(dir); 83 } 84 catch { } 85 86 if (files != null && files.Length > 0) 87 { 88 fileList.AddRange(files); 89 } 90 91 string[] folders = null; 92 try 93 { 94 folders = Directory.GetDirectories(dir); 95 } 96 catch { } 97 98 if (folders != null && folders.Length > 0) 99 { 100 foreach (string folder in folders) 101 { 102 stack.Push(folder); 103 } 104 } 105 } 106 } 107 108 /// <summary> 109 /// 隊列法 110 /// </summary> 111 /// <param name="path"></param> 112 private static void FunctionQueue(string path, ref List<string> fileList) 113 { 114 Queue<string> queue = new Queue<string>(); 115 116 queue.Enqueue(path); 117 118 while (queue.Count > 0) 119 { 120 string dir = queue.Dequeue(); 121 122 string[] files = null; 123 try 124 { 125 files = Directory.GetFiles(dir); 126 } 127 catch { } 128 129 if (files != null && files.Length > 0) 130 { 131 fileList.AddRange(files); 132 } 133 134 string[] folders = null; 135 try 136 { 137 folders = Directory.GetDirectories(dir); 138 } 139 catch { } 140 141 if (folders != null && folders.Length > 0) 142 { 143 foreach (string folder in folders) 144 { 145 queue.Enqueue(folder); 146 } 147 } 148 } 149 } 150 } 151 }
請仔細對比下三種循環結構的寫法,特別注意下里面有用到 if(true){...} ,這種方式在某些編輯環境中能夠產生很是美妙的效果,你造嗎?數據庫
好了,就這麼多吧,實在沒時間寫新東西了。之後出貼應該很慢了,不過會出點服務器集羣方面的帖子,這些帖子現存的已經不少,可是大部分都是視圖文字說明,對於新手來講很難掌握,我本身曾經摸索的時候,但是煞費苦心啊。它須要的條件很是苛刻,須要大量服務器、高併發訪問、以及核心數據庫和分化數據庫策略等等,若是用一帖把它給解釋清楚,我感受就是天方夜譚,它徹底能夠出成一本書,因此在一帖中講解集羣使用圖文方式是最好的方式,比較全面歸納,因此我沒怪過前輩們寫集羣方面的知識寫的太籠統,徹底不明因此。前輩們寫這麼高明的帖子是給有心人看的,他想把他的思想教給咱們;我寫帖子徹底是給屌絲看的,作個知識的入門引導吧,呵呵。(預先掌握golang,erlang編程)編程