处理文本文件时CPU利用率达到100%

时间:2013-07-09 03:14:25

标签: c# performance optimization file-io

我有一个拥有超过300000条记录的主帐户文件。此文件包含Office,FA,UUID字段以及其他字段。我们有一个网络服务,可以回收当前注册的试点办公室。

我需要生成一个输出文件,其中包含来自主帐户文件的唯一UUID,这些文件是导航的一部分。以下是我的代码。

public class Job
{
    static Dictionary<string, string> UUIDOfficeFA = new Dictionary<string, string>();
    static Dictionary<string, string> UUIDs = new Dictionary<string, string>();
    static string PilotOfficeList = string.Empty;

    /// <summary>
    /// 
    /// </summary>
    public void RunJob()
    {
        try
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(ConfigurationSettings.AppSettings["AccountFileFolder"]);
            if (directoryInfo != null || directoryInfo.Exists == false)
            {
                FileInfo[] files = directoryInfo.GetFiles();
                DateTime lastWrite = DateTime.MinValue;
                FileInfo lastWritenFile = null;
                foreach (FileInfo file in files)
                {
                      if (file.LastWriteTime > lastWrite)
                        {
                            lastWrite = file.LastWriteTime;
                            lastWritenFile = file;
                        }

                }
                if (lastWritenFile != null)
                {
                   if (RetrieveUUIDs(lastWritenFile))
                    {
                        WriteFile();
                    }
                }
                else
                {
                    throw new Exception("No matching account file found for processing");
                }
            }
        }
        catch (Exception ex)
        {
           throw ex;
        }

    }

    static void WriteFile()
    {
       try
        {

            string FileName = "Testfile_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";
            string Path = ConfigurationSettings.AppSettings["TestFolder"] + @"\" + FileName;

            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, string> kvp in UUIDs)
            {
                sb.AppendLine(kvp.Value);
            }
            using (StreamWriter writer = File.AppendText(Path))
            {
                writer.Write(sb.ToString());
                writer.Flush();
            }


        }
        catch (Exception ex)
        {
           throw ex;
        }

    }


  static bool RetrieveUUIDs(FileInfo file)
    {
       try
        {

            using (StreamReader sr = file.OpenText())
            {

                string accountFileData = null;
                accountFileData = sr.ReadToEnd();

                string[] str = accountFileData.Trim().Split(new string[] { "\n" }, StringSplitOptions.None);

                List<string> AccountInfo = new List<string>(str);

                if (AccountInfo.Count > 0)
                {
                    TestWebservice client = new TestWebservice();
                    GetBrancheListRequest request = new GetBrancheListRequest();
                    GetBrancheListResponse response =client.GetBrancheList(request);

                    if (response != null)
                    {
                        PilotOfficeList = response.GetBrancheListResult;

                        if (string.IsNullOrEmpty(PilotOfficeList))
                        {
                            throw new Exception("No branch is enrolled for pilot");
                        }
                    }
                }

                foreach (string accountInfo in AccountInfo)
                {
                    RetrieveUUID(accountInfo);
                }


            }
        }
        catch (Exception ex)
        {
          throw ex;
        }
       return true;
    }

    private static void RetrieveUUID(string line)
    {
        try
        {
            string UUID = line.Substring(2, 50).Trim();
            string Office = line.Substring(444, 3).Trim();
            string FA = line.Substring(447, 3).Trim();
            if (!string.IsNullOrEmpty(PilotOfficeList))
            {
                if (!string.IsNullOrEmpty(UUID) || !string.IsNullOrEmpty(Office))
                {
                    if (PilotOfficeList.IndexOf(Office) > -1)
                    {
                        if (!UUIDOfficeFA.ContainsKey(UUID + Office + FA))
                        {
                            UUIDOfficeFA.Add(UUID + Office + FA, UUID + Office + FA);
                            if (!UUIDs.ContainsKey(UUID))
                            {
                                UUIDs.Add(UUID, UUID);
                            }

                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

}

问题是当这个程序运行时,CPU利用率达到100%。尽管整个作业在2分钟内完成但我无法在服务器上移动它,因为它可能会导致其他应用程序出现问题。请建议我如何优化这一点,以便CPU不会达到100%。

非常感谢您以前的帮助。

注意 - 以下是几个主要的CPU利用率

1-
accountFileData = sr.ReadToEnd();
2-
string[] str = accountFileData.Trim().Split(new string[] { "\n" },    StringSplitOptions.None);
3-
foreach (string accountInfo in AccountInfo)
{
         RetrieveUUID(accountInfo);
}

0 个答案:

没有答案