Tag: 性能

数组包含性能

我有一个与数组一起工作的程序,在某些时候我必须检查一个值是否在数组中,这个函数占用了大约70%的程序CPU时间,所以我想知道是否有办法更有效地做到这一点。 这些是我的function: private static int[] GenerateRow(int length, Random RNG) { int[] row = InitalizeRow(length); int index = 0; while (!AreAllNumbersGenerated(row)) { int value = RNG.Next(0, length); if (!RowContains(row, value)) { row[index] = value; index++; } } return row; } private static bool AreAllNumbersGenerated(int[] row) { for (int i = 0; i < row.Length; i++) if(!RowContains(row, […]

DataGrid Cell.Style绑定

我有WPF DataGrid(.net 4.0)的性能问题 首先,一些细节: 我有一个带有Observable集合的datagrid作为ItemsSource。 这个observableCollection本身包含对象的集合,每个集合因此是一行,每个对象都是一个单元格(当然是“逻辑”单元格,而不是实际的dataGridCell) 我这样做的原因是因为我只在运行时知道我将在dataGrid中有多少列。 然后我将每个DataGridCell的值绑定到“逻辑”表中的对象的值(=集合的集合) 现在我遇到的麻烦是我还必须能够在应用程序运行时随时更改任何单元格的属性(如Background,Foreground,FontFamily等等)。 我想出的解决方案是将列的’cellStyles设置为绑定到“逻辑”单元属性的绑定 这是一个示例代码(我的应用程序中没有Xaml): public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Width = 1200; Height = 780; Top = 60; Left = 200; DataGrid dg = new DataGrid(); Content = dg; ObservableCollection Source = new ObservableCollection(); dg.ItemsSource = Source; dg.SelectionMode = DataGridSelectionMode.Extended; dg.IsSynchronizedWithCurrentItem = […]

写文件需要针对繁重的流量第3部分进行优化

这个问题是前两部分的延续,任何有兴趣看到我来自你的人都可以参考第1部分和第2部分,但没有必要。 写文件需要针对繁重的流量进行优化 写文件需要针对繁重的流量第2部分进行优化 现在我有一个工作片段,相关部分如下: public static class memoryStreamClass { static MemoryStream ms1 = new MemoryStream(); public static void fillBuffer(string outputString) { byte[] outputByte = Encoding.ASCII.GetBytes(outputString); ms1.Write(outputByte, 0, outputByte.Length); if (ms1.Length > 8100) { emptyBuffer(ms1); ms1.SetLength(0); ms1.Position = 0; } } static void emptyBuffer(MemoryStream ms) { FileStream outStream = new FileStream(“c:\\output.txt”, FileMode.Append); ms.WriteTo(outStream); outStream.Flush(); outStream.Close(); } […]

有什么办法可以让这个C#代码更快吗?

我正在读取一个大文件X12并解析其中的信息。 我有两个瓶颈function,似乎无法解决。 read_line()和get_element()有什么方法可以让这两个函数更快? get_element函数的主要瓶颈似乎是Substring方法。 public String get_element(int element_number) { int count = 0; int start_index = 0; int end_index = 0; int current_index = 0; while (count < element_number && current_index != -1) { current_index = line_text.IndexOf(x12_reader.element_delimiter, start_index); start_index = current_index + 1; count++; } if (current_index != -1) { end_index = line_text.IndexOf(x12_reader.element_delimiter, start_index); if […]

C#性能分析 – 如何计算CPU周期?

这是进行性能分析的有效方法吗? 我希望获得纳秒精度并确定类型转换的性能: class PerformanceTest { static double last = 0.0; static List numericGenericData = new List(); static List numericTypedData = new List(); static void Main(string[] args) { double totalWithCasting = 0.0; double totalWithoutCasting = 0.0; for (double d = 0.0; d < 1000000.0; ++d) { numericGenericData.Add(d); numericTypedData.Add(d); } Stopwatch stopwatch = new Stopwatch(); for (int […]

sharpziplib – 你可以添加一个文件而不先复制整个zip吗?

我试图使用sharpziplib将文件添加到现有的.zip文件 – 问题是,zip文件大小为1GB。 当我尝试添加1个小文件(400k)时,sharpziplib会在添加新文件之前创建orig zip文件的副本/ temp – 当可用磁盘空间量小于您尝试的zip文件的2倍时,这会出现问题更新。 例如:1GB zip myfile.zip 1GB zip myfile.zip.tmp.293 ZipFile zf = new ZipFile(path); zf.BeginUpdate(); zf.Add(file); // Adding a 400k file here causes a 1GB temp file to be created zf.EndUpdate(); zf.Close(); 有没有更有效的方法来做到这一点? 谢谢 :-)

系统启动/重新启动时,WMI调用会花费太多时间

我想获得Windows服务的路径, var managementObjectSearcher = new ManagementObjectSearcher(“Select * from Win32_Service where serviceName = MyService”); 这段代码是由其他一些服务的构造函数编写的…… 当系统启动并运行时,一切正常,但是如果我重新启动系统,则为此调用设置StopWatch类,并显示它显示该服务因此调用需要35-45秒才能启动。 任何提高系统重启性能的建议……

c#使用命名空间语句排序

我以为我读到某个地方,订购你的使用陈述并摆脱未使用的陈述有一些性能上的好处……但我似乎无法找到任何证据或资源来支持这个……这有什么道理吗?

复杂对象图的快速HashCode

我有一个非常复杂的对象 ,我需要获得这些对象的唯一性 。 可以通过重写GetHashCode()来完成一个解决方案。 我已经实现了以下代码: public override int GetHashCode() { return this._complexObject1.GetHashCode() ^ this._complexObject2.GetHashCode() ^ this._complexObject3.GetHashCode() ^ this._complexObject4.GetHashCode() ^ this._complexObject5.GetHashCode() ^ this._complexObject6.GetHashCode() ^ this._complexObject7.GetHashCode() ^ this._complexObject8.GetHashCode(); } 这些复杂对象也会覆盖 GetHashCode()并执行类似的操作 。 我的项目需要这些对象的唯一性,我经常处理这些对象,内部数据也会以各种方式和位置 发生变化 。 我需要一种更快的方法来找到这些复杂对象的唯一性,这需要考虑性能和内存 。 提前致谢 穆奈姆

搜索列表表示字符串.StartsWith()

我有一个 List 有1500个字符串。 我现在使用以下代码只提取以字符串prefixText开头的字符串。 foreach(string a in ) { if(a.StartsWith(prefixText, true, null)) { newlist.Add(a); } } 这很快,但我正在寻找谷歌快速。 现在我的问题是,如果我按字母顺序排列List,那么比较char by char我可以加快速度吗? 或者其他任何有关加快速度的建议?