欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#避免回溯方法心得

 更新時(shí)間:2014年09月15日 10:10:07   投稿:shichen2014  
這篇文章主要介紹了C#避免回溯方法,以實(shí)例的形式講述了回溯方法的弊端及解決處理方法,是非常實(shí)用的技巧,需要的朋友可以參考下

本文實(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)文章

最新評(píng)論