C#避免回溯方法心得
脚本之家 / 编程助手:解决程序员“几乎”所有问题!
脚本之家官方知识库 → 点击立即使用
本文实例讲述了C#避免回溯方法,分享给大家供大家参考之用。具体分析如下:
首先,回溯法是不可控的,有时候会超出我们意料之外产生不妙的结果,最常见的也就是内存泄漏。。
回溯方法是很容易想到,又不容易想到的,往往,我们思维更容易进入的是回溯法。但是回溯法有着它的弊端,非常明显的弊端是作用域内产生的变量和引用在回溯法调用未完成时,不能释放(对于大部分编辑器来说,排除有着优化能力的编辑器)。如果我们在某一方法中使用极多的回溯调用,在方法中不能及时的对方法作用域内的变量和引用释放,最终会造成内存不足和cpu的计算负荷增大(内存机制中可以将过剩的数据转存到虚拟内存、硬盘,这个就不说了)。使用栈(队)式的循环,可以轻易避免回溯法,而且栈(队)式的数据再使用之后可以很方便的抛出移除。某些时候就是这样,一个小小的改动,可以让一个程序在某种特定的环境中起死回生。(之前做过一个数独运算器的算法,后来的优化改进就是为了避免回溯)
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace 避免回溯方法 { class Program { static void Main( string [] args) { string path = AppDomain.CurrentDomain.BaseDirectory; List< string > fileList1 = new List< string >(); FunctionHuishuo(path, ref fileList1); List< string > fileList2 = new List< string >(); FunctionQueue(path, ref fileList2); List< string > fileList3 = new List< string >(); FunctionStack(path, ref fileList3); } /// <summary> /// 回溯法 /// </summary> /// <param name="path"></param> /// <param name="fileList"></param> private static void FunctionHuishuo( string path, ref List< string > fileList) { if ( true ) { string [] files = null ; try { files = Directory.GetFiles(path); } catch { } if (files != null && files.Length > 0) { fileList.AddRange(files); } } if ( true ) { string [] folders = null ; try { folders = Directory.GetDirectories(path); } catch { } if (folders != null && folders.Length > 0) { foreach ( string folder in folders) { FunctionHuishuo(folder, ref fileList); } } } } /// <summary> /// 堆栈法 /// </summary> /// <param name="path"></param> private static void FunctionStack( string path, ref List< string > fileList) { Stack< string > stack = new Stack< string >(); stack.Push(path); while (stack.Count > 0) { string dir = stack.Pop(); string [] files = null ; try { files = Directory.GetFiles(dir); } catch { } if (files != null && files.Length > 0) { fileList.AddRange(files); } string [] folders = null ; try { folders = Directory.GetDirectories(dir); } catch { } if (folders != null && folders.Length > 0) { foreach ( string folder in folders) { stack.Push(folder); } } } } /// <summary> /// 队列法 /// </summary> /// <param name="path"></param> private static void FunctionQueue( string path, ref List< string > fileList) { Queue< string > queue = new Queue< string >(); queue.Enqueue(path); while (queue.Count > 0) { string dir = queue.Dequeue(); string [] files = null ; try { files = Directory.GetFiles(dir); } catch { } if (files != null && files.Length > 0) { fileList.AddRange(files); } string [] folders = null ; try { folders = Directory.GetDirectories(dir); } catch { } if (folders != null && folders.Length > 0) { foreach ( string folder in folders) { queue.Enqueue(folder); } } } } } } |
请仔细对比下三种循环结构的写法,特别注意下里面有用到 if(true){...} ,这种方式在某些编辑环境中可以产生非常美妙的效果。
相信本文所述对大家C#程序设计的学习有一定的借鉴价值。

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
相关文章
C#使用HttpWebRequest与HttpWebResponse模拟用户登录
这篇文章主要为大家详细介绍了C#使用HttpWebRequest与HttpWebResponse模拟用户登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04C# ArrayList、HashSet、HashTable、List、Dictionary的区别详解
这篇文章主要介绍了C# ArrayList、HashSet、HashTable、List、Dictionary的区别的相关知识点内容,有需要朋友们参考下。2019-08-08c# 自定义值类型一定不要忘了重写Equals,否则性能和空间双双堪忧
这篇文章主要介绍了c# 自定义值类型一定不要忘了重写Equals,帮助大家提高c# 程序的性能,感兴趣的朋友可以了解下2020-08-08
最新评论