"使用 System; "\n"using System.Collections.Generic; "\n"using System.IO; "\n"using System.Linq; "\n"using System.Net.Mime; "\n"using System.Security.Cryptography.X509Certificates; "\n"using System.Text; "\n"using System.Threading; "\n"using System.Threading.Tasks; "\n"\n"namespace 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>> data = new Dictionary<int, Dictionary<string, List>>();//最终数据存储区 "\n" public static Dictionary<string, List> temp = new Dictionary<string, List>();//临时数据,待该次完成后合并到data "\n" public static Dictionary<string, List> solveing = new Dictionary<string, List>();//正在处理的数据 "\n" public static List threads = new List(); "\n"\n" public static int num = 0; "\n" public static int threadSpace = 100000; "\n" public static List StartPoint = new List(); "\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()/"1") "\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 (StartPoint.Count == 0) "\n" { "\n" Thread thread = new Thread(CharScanThread); "\n" threads.Add(thread); "\n" thread.Start(); "\n" } "\n" else if (StartPoint[StartPoint.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" ( "\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" ( "\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 (StartPoint.Count > 0) "\n" { "\n" point = StartPoint[StartPoint.Count - 1] + threadSpace; "\n" } "\n" StartPoint.Add(point); "\n" for (int i = point; i < text.Length && (i == point || !StartPoint.Contains(i)); i++) "\n" { "\n" if (!temp.ContainsKey(text[i].ToString())) "\n" try "\n" { "\n" temp.Add(text[i].ToString(), new List(SearchString(text, text[i].ToString()))); "\n" } "\n" catch "\n" { "\n"\n" } "\n" num++; "\n" } "\n" if (StartPoint.Count == 0) "\n" { "\n" Thread thread = new Thread(CharScanThread); "\n" threads.Add(thread); "\n" thread.Start(); "\n" } "\n" else if (StartPoint[StartPoint.Count - 1] + (threadSpace * 2) < text.Length) "\n" { "\n" Thread thread = new Thread(CharScanThread); "\n" threads.Add(thread); "\n" thread.Start(); "\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 result = new List(); "\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" if (t) result.Add(i); "\n" } "\n" return result.ToArray(); "\n" } "\n" } "\n"} "\n"此段C#代码存在以下问题: "\n"\n"1. 代码缺少注释:没有对各个函数、变量和逻辑进行注释,不利于代码的阅读和理解。 "\n"\n"2. 变量命名不规范:变量名使用了一些不易理解的命名,如positiontempsolveing等,不符合命名规范,建议使用更加具有描述性的变量名。 "\n"\n"3. 异常处理不完善:在try-catch块中捕获异常后,只打印了异常信息,没有进行具体的异常处理,不利于问题的排查和修复。 "\n"\n"4. 多线程处理不安全:在CharScanThread函数中,对共享变量temp的操作可能存在线程安全问题,多个线程同时访问temp时可能会引发竞争条件,需要使用线程同步机制来保证数据的一致性。 "\n"\n"5. SearchString函数存在性能问题:在SearchString函数中,使用了嵌套循环来逐个比较字符,算法复杂度较高,可以考虑使用更高效的字符串匹配算法来优化性能。 "\n"\n"6. 代码结构混乱:代码中各个函数的调用关系和逻辑关系不清晰,代码结构混乱,不易于维护和扩展。可以考虑对代码进行重构,将各个功能模块进行拆分和封装,提高代码的可读性和可维护性。 "\n"

C# 代码分析:性能、线程安全和代码可读性问题

原文地址: https://www.cveoy.top/t/topic/pFx9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录