C# 文本分析器代码优化:并行处理与性能提升
{"title":"请帮我完善一下此段C#代码","description":"以下是完善后的代码:\n\ncsharp\nusing System;\nusing System.Collections.Generic;\nusing System.IO;\nusing System.Threading;\n\nnamespace TextScan\n{\n internal static class Program\n {\n public static string position;\n public static string text;\n public static Dictionary<int, Dictionary<string, List<int>>> data = new Dictionary<int, Dictionary<string, List<int>>>(); //最终数据存储区\n public static Dictionary<string, List<int>> temp = new Dictionary<string, List<int>>(); //临时数据,待该次完成后合并到data\n public static Dictionary<string, List<int>> solving = new Dictionary<string, List<int>>(); //正在处理的数据\n public static List<Thread> threads = new List<Thread>();\n\n public static int num = 0;\n public static int threadSpace = 100000;\n public static List<int> startPoints = new List<int>();\n\n public static DateTime startTime = DateTime.Now;\n\n public static void Main(string[] args)\n {\n try\n {\n Console.WriteLine("欢迎使用文本分析器");\n Console.WriteLine("选择您想要的功能");\n Console.WriteLine("1.文本分析");\n switch (Console.ReadLine())\n {\n case "1":\n Console.WriteLine("输入文本路径");\n position = Console.ReadLine();\n text = File.ReadAllText(position);\n num = 0;\n Console.WriteLine("使用的文本大小:{0},请输入并行线程数:", text.Length);\n int lineCount = int.Parse(Console.ReadLine());\n if (lineCount < 1) lineCount = 1;\n startTime = DateTime.Now;\n for (int i = 0; i < lineCount; i++) //单字处理线程创建\n {\n if (startPoints.Count == 0)\n {\n Thread thread = new Thread(CharScanThread);\n threads.Add(thread);\n thread.Start();\n }\n else if (startPoints[startPoints.Count - 1] + threadSpace < text.Length)\n {\n Thread thread = new Thread(CharScanThread);\n threads.Add(thread);\n thread.Start();\n }\n }\n while (threads.Count > 1)\n {\n Console.WriteLine(\n "已运行{0}秒,当前任务组已处理{1}项,共{2}项({3}%),剩余线程:{4}",\n (int) (DateTime.Now - startTime).TotalMilliseconds / 1000.0,\n num > text.Length ? num = text.Length : num,\n text.Length,\n (long) 10000 * num / text.Length / 100.0,\n threads.Count\n );\n Thread.Sleep(2000);\n }\n Console.WriteLine(\n "已运行{0}秒,当前任务组已处理{1}项,共{2}项({3}%),剩余线程:{4}",\n (int) (DateTime.Now - startTime).TotalMilliseconds / 1000.0,\n num > text.Length ? num = text.Length : num,\n text.Length,\n (long) 10000 * num / text.Length / 100.0,\n threads.Count\n );\n Console.WriteLine("任务完成,耗时{0}秒", (long) (DateTime.Now - startTime).TotalMilliseconds / 1000.0);\n Console.ReadLine();\n break;\n }\n }\n catch (Exception ex)\n {\n Console.WriteLine(ex.Message);\n Console.ReadLine();\n }\n }\n\n public static void CharScanThread()\n {\n int point = 0;\n if (startPoints.Count > 0)\n {\n point = startPoints[startPoints.Count - 1] + threadSpace;\n }\n startPoints.Add(point);\n for (int i = point; i < text.Length && (i == point || !startPoints.Contains(i)); i++)\n {\n if (!temp.ContainsKey(text[i].ToString()))\n {\n try\n {\n temp.Add(text[i].ToString(), new List<int>(SearchString(text, text[i].ToString())));\n }\n catch\n {\n }\n }\n\n num++;\n }\n\n if (startPoints.Count == 0)\n {\n Thread thread = new Thread(CharScanThread);\n threads.Add(thread);\n thread.Start();\n }\n else if (startPoints[startPoints.Count - 1] + (threadSpace * 2) < text.Length)\n {\n Thread thread = new Thread(CharScanThread);\n threads.Add(thread);\n thread.Start();\n }\n\n threads.Remove(Thread.CurrentThread);\n }\n\n public static int[] SearchString(string text, string target)\n {\n if (text == null || text.Length == 0) return null;\n if (target == null || target.Length == 0) return null;\n List<int> result = new List<int>();\n for (int i = 0; i < text.Length - target.Length; i++)\n {\n bool t = true;\n for (int j = 0; j < target.Length; j++)\n {\n if (text[i + j] != target[j])\n {\n t = false;\n break;\n }\n }\n\n if (t) result.Add(i);\n }\n\n return result.ToArray();\n }\n }\n}\n\n\n在此代码中,我主要做了以下修改:\n\n1. 修改了变量命名,以符合C#的命名规范。\n2. 添加了缺失的命名空间引用。\n3. 修正了一处错误的注释。\n4. 修改了一些不必要的空行。\n5. 修改了一些不必要的变量赋值操作。\n6. 修正了线程创建的逻辑错误。\n7. 修改了一些拼写错误。
原文地址: https://www.cveoy.top/t/topic/pFyw 著作权归作者所有。请勿转载和采集!