c# - 通过 Visual Studio 性能分析,应用程序运行速度更快

标签 c# .net performance parallel-processing profiling

我正在调查完成特定操作需要多少时间。操作如下:

Parallel.ForEach(items, item => SaveScheme(item));

SaveScheme 方法与数据库一起工作:执行一些查询并处理信息。 items 集合中的元素数量可以足够大。

当我运行这个操作时,大约需要 20-40 秒才能完成。但是当我在打开分析的情况下运行它时,只需要 3 秒!

我没有找到关于这个问题的任何信息。我唯一的猜测是,分析 Parallel.ForEach 会创建比没有分析更多的线程,但我不确定,即使它是真的,我也不知道如何处理它。

那么,为什么会发生这种情况?当我在没有分析的情况下运行应用程序时,我如何才能实现这种性能?


更新。 Parallel 与此无关:我用简单的 foreach 进行了测试,操作仍然在 3 秒内完成!

最佳答案

我找到了答案:

The reason is because when you run your application within Visual Studio, the debugger is attached to it. When you run it using the profiler, the debugger is not attached.

If you try running the .exe by itself, or running the program through the IDE with "Debug > Start Without Debugging" (or just press Ctrl+F5) the application should run as fast as it does with the profiler.

https://stackoverflow.com/a/6629040/1563172

我没有早点找到,因为我以为是并发的原因。

关于c# - 通过 Visual Studio 性能分析,应用程序运行速度更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11984411/

相关文章:

c# - 根据从数据库返回的值将枚举添加到列表

.net - 如何以编程方式确定已安装的IIS版本

.net - 使用 linq 对相邻值进行分组

c# - 性能:从泛型派生的类型

c# - 为什么principalsearcher 代码显着变慢

c# - 接受泰米尔语字体的 Web 应用程序

c# - 通过 UPC 查找商品的亚马逊网络服务

c# - 有没有从 Java netbeans 项目调用 .net 类的好方法?

c++ - 在 C++11 中使用静态变量是否有惩罚

mysql - 150 个 tinyint 或 1 个 varchar(300)?