Tag: 性能

C使用正则表达式搜索Sharp文件夹

从顶级目录获取与某个正则表达式匹配的文件夹列表的最有效方法是什么? 我目前只是递归迭代子文件夹以查看它们是否与正则表达式匹配,如果它们匹配,我将使用目录路径获取文件名。 目前,由于此目录中的文件夹数量,此搜索大约需要50分钟才能使用当前方法。 private void ProcessFiles(string path, string searchPattern) { string pattern = @”^(\\\\server\\folder1\\subfolder\\(MENS|WOMENS|MENS\sDROPBOX|WOMENS\sDROPBOX)\\((((COLOR\sCHIPS)|(ALL\sMENS\sCOLORS)))|((\d{4})\\(\w+)\\(FINAL\sART|FINAL\sARTWORK)\\(\d{3}))))$”; DirectoryInfo di = new DirectoryInfo(path); try { Debug.WriteLine(“I’m in ” + di.FullName); if (di.Exists) { DirectoryInfo[] dirs = di.GetDirectories(“*”, SearchOption.TopDirectoryOnly); foreach (DirectoryInfo d in dirs) { string[] splitPath = d.FullName.Split(‘\\’); var dirMatch = new Regex(pattern, RegexOptions.IgnoreCase); if (dirMatch.IsMatch(d.FullName)) { Debug.WriteLine(“—Processing Directory: […]

我怎样才能加快这个直方图课程的速度?

这应该计算8位灰度图像的直方图。 使用1024×770测试位图,CreateTime最终在890ms左右。 我怎样才能更快地完成这个(方式,方式)? 编辑:我应该提到,这实际上并没有计算直方图,它只从位图中获取值。 所以我真的应该问,从8位灰度图像中检索所有像素值的最快方法是什么? public class Histogram { private static int[,] values; public Histogram(Bitmap b) { var sw = Stopwatch.StartNew(); values = new int[b.Width, b.Height]; for (int w = 0; w < b.Width; ++w) { for (int h = 0; h < b.Height; ++h) { values[w, h] = b.GetPixel(w, h).R; } } sw.Stop(); CreateTime […]

更有效的积分循环

public double Integral(double[] x, double intPointOne, double intPointTwo) { double integral = 0; double i = intPointOne; do { integral += Function(x[i])*.001; i = i + .001; } while (i <= intPointTwo); return integral; } 这是一个函数,我必须简单地使用部分的总和来集成x1-x2中的函数。 如何使这个循环更有效(使用更少的循环),但更准确? Function在每次迭代中都会发生变化,但它应该是无关紧要的,因为它的数量级(或边界)应保持相对相同…

HttpWebRequest到c#中的web服务; 如何从响应流中获取数据?

我正在使用POST调用Web服务并接收2MB xml。 问题是我需要花费很多时间才能使用Stream中的数据。 响应似乎是在7秒之后,但是从响应流中读取内容(它是一个字符串)还需要10秒。 Stopwatch s = new Stopwatch(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MyUri); req.Method = “POST”; req.ContentType = “application/x-www-form-urlencoded”; req.ContentLength = Poststring.Length; s.Start(); StreamWriter swriter = new StreamWriter(req.GetRequestStream()); swriter.Write(Poststring); swriter.Close(); // Get the response. 7 sec HttpWebResponse response = (HttpWebResponse)req.GetResponse(); s.Stop(); Debug.WriteLine(“Talking to Web-Service: “+s.ElapsedMilliseconds); s.Reset(); // Get the stream containing content returned by the […]

避免VBCSCompiler对Roslyn驱动的ASP.NET Razor MVC视图的影响?

为了在MVC5的Razor视图中支持C#6,我们通过web.config打开了Roslyn编译器平台: 但是,在生产部署之后,每个视图/控制器似乎都有明显的“第一次加载”延迟,这比没有启用此编译器时更糟糕。 重要的是,此延迟是您从部署的新站点获得的常规JIT延迟的补充。 页面显然较慢,而VBCSCompiler.exe似乎在后台运行以“进一步编译”这些页面。 是否有预编译/优化此情况的最佳实践,以消除部署后的首次加载运行时延迟? 理想情况下,VBCSCompiler.exe在部署发生后未运行,并在构建时执行。 我已经看到了aspnet_compiler.exe的提及,并遇到了StackExchange.Precompilation(请参阅https://blog.stackoverflow.com/2015/07/announcing-stackexchange-precompilation/ ),并想知道这是否是正确的修复程序。 有没有人对这个特殊问题有任何经验? 谢谢。

C#.First()vs

有兴趣,方法有什么不同。 所以,我创建了两个片段。 Snippet A List a = new List(); a.Add(4); a.Add(6); int b = a.First(); 和 Snippet B List a = new List(); a.Add(4); a.Add(6); int b = a[0]; 在IL,我们相信,所以 Snippet A IL IL_0000: nop IL_0001: newobj System.Collections.Generic.List..ctor IL_0006: stloc.0 // a IL_0007: ldloc.0 // a IL_0008: ldc.i4.4 IL_0009: callvirt System.Collections.Generic.List.Add IL_000E: nop IL_000F: ldloc.0 […]

PerSession与PerCall

决定是使用PerSession还是使用PerCall的一般经验法则是什么? 我有一个稍微沉重的(我认为..)WCF服务,包含大约80个表的CRUD方法。 我已将WCF服务分为1个服务中的7个合同(即1个服务中的7个端点),这样每个合同都会处理自己的域,例如我有一个销售合同,所以所有与销售相关的表,以及相应的操作属于销售“有界环境” 所以我的WCF服务结构看起来像这样: public partial class ABCService : ISalesService { //CRUD methods of all tables related to Sales } public partial class ABCService : IMarketingService { //CRUD methods of all tables related to Marketing } public partial class ABCService : ICustomerService { //CRUD methods of all tables related to Customer } public partial class […]

结构类型数组的性能

例: // Potentially large struct. struct Foo { public int A; public int B; // etc. } Foo[] arr = new Foo[100]; 如果Foo是100字节结构,则在执行以下语句期间将在内存中复制多少字节: int x = arr[0].A 也就是说,将arr [0]计算为某个临时变量(Foo实例的100字节副本),然后将.A复制到变量x(4字节副本)。 或者是编译器,JITer和CLR的某种组合能够优化此语句,以便将A的4个字节直接复制到x 。 如果执行优化,当项目保存在List或者数组作为IList或ArraySegment传递时,它是否仍然成立?

在C#中搜索子目录

我有一个文件名列表,我想搜索一个目录及其所有子目录。 这些目录每个包含大约200,000个文件。 我的代码找到了该文件,但每个文件大约需要20分钟。 有人可以提出更好的方法吗? 代码片段 String[] file_names = File.ReadAllLines(@”C:\file.txt”); foreach(string file_name in file_names) { string[] files = Directory.GetFiles(@”I:\pax\”, file_name + “.txt”, SearchOption.AllDirectories); foreach(string file in files) { System.IO.File.Copy(file, @”C:\” + textBox1.Text + @”\N\O\” + file_name + “.txt” ); } }

是IEnumerable.Any比rest的for循环快吗?

我们在打开表单的代码中经历了一些缓慢,这可能是由于一个带有break的for循环需要很长时间才能执行。 我将其切换为IEnumerable.Any()并且看到表单很快打开。 我现在试图弄清楚单独进行此更改是否会提高性能,还是更有效地访问ProductIDs属性。 这种实施应该更快,如果是这样,为什么? 原始实施: public bool ContainsProduct(int productID) { bool containsProduct = false; for (int i = 0; i < this.ProductIDs.Length; i++) { if (productID == this.ProductIDs[i]) { containsProduct = true; break; } } return containsProduct; } 新实施: public bool ContainsProduct(int productID) { return this.ProductIDs.Any(t => productID == t); }