C#避免回溯方法心得
本文實(shí)例講述了C#避免回溯方法,分享給大家供大家參考之用。具體分析如下:
首先,回溯法是不可控的,有時(shí)候會(huì)超出我們意料之外產(chǎn)生不妙的結(jié)果,最常見的也就是內(nèi)存泄漏。。
回溯方法是很容易想到,又不容易想到的,往往,我們思維更容易進(jìn)入的是回溯法。但是回溯法有著它的弊端,非常明顯的弊端是作用域內(nèi)產(chǎn)生的變量和引用在回溯法調(diào)用未完成時(shí),不能釋放(對(duì)于大部分編輯器來說,排除有著優(yōu)化能力的編輯器)。如果我們?cè)谀骋环椒ㄖ惺褂脴O多的回溯調(diào)用,在方法中不能及時(shí)的對(duì)方法作用域內(nèi)的變量和引用釋放,最終會(huì)造成內(nèi)存不足和cpu的計(jì)算負(fù)荷增大(內(nèi)存機(jī)制中可以將過剩的數(shù)據(jù)轉(zhuǎn)存到虛擬內(nèi)存、硬盤,這個(gè)就不說了)。使用棧(隊(duì))式的循環(huán),可以輕易避免回溯法,而且棧(隊(duì))式的數(shù)據(jù)再使用之后可以很方便的拋出移除。某些時(shí)候就是這樣,一個(gè)小小的改動(dòng),可以讓一個(gè)程序在某種特定的環(huán)境中起死回生。(之前做過一個(gè)數(shù)獨(dú)運(yùn)算器的算法,后來的優(yōu)化改進(jìn)就是為了避免回溯)
示例代碼如下:
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>
/// 隊(duì)列法
/// </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);
}
}
}
}
}
}
請(qǐng)仔細(xì)對(duì)比下三種循環(huán)結(jié)構(gòu)的寫法,特別注意下里面有用到 if(true){...} ,這種方式在某些編輯環(huán)境中可以產(chǎn)生非常美妙的效果。
相信本文所述對(duì)大家C#程序設(shè)計(jì)的學(xué)習(xí)有一定的借鑒價(jià)值。
相關(guān)文章
winform c#中子窗體關(guān)閉刷新父窗體的實(shí)例
下面小編就為大家?guī)硪黄獁inform c#中子窗體關(guān)閉刷新父窗體的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
C#讀取多條數(shù)據(jù)記錄導(dǎo)出到Word之圖片輸出改造
這篇文章主要為大家詳細(xì)介紹了C#讀取多條數(shù)據(jù)記錄并導(dǎo)出到Word標(biāo)簽?zāi)0逯械膱D片輸出問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11
C#中datagridview使用tooltip控件顯示單元格內(nèi)容的方法
這篇文章主要介紹了C#中datagridview使用tooltip控件顯示單元格內(nèi)容的方法,實(shí)例分析了C#控件的相關(guān)使用技巧,需要的朋友可以參考下2016-06-06
winform 使用Anchor屬性進(jìn)行界面布局的方法詳解
這篇文章主要介紹了winform 使用Anchor屬性進(jìn)行界面布局的方法,有需要的朋友可以參考一下2013-12-12

