问题描述
我正在调查完成一项特定操作需要花费多少时间.操作如下:
I am investigating for how many time it takes for a particular operation to complete. The operation is like the following:
Parallel.ForEach(items, item => SaveScheme(item));
SaveScheme
方法适用于数据库:执行一些查询并处理信息. items
集合中的元素数量可能足够大.
The SaveScheme
method works with a database: executes some queries and works with the information. The amount of elements in items
collection can be big enough.
当我运行此操作时,大约需要20-40秒才能完成.但是,当我在启用概要分析的情况下运行它时,仅需3秒钟!
When I run this operation, it takes about 20-40 seconds to complete. But when I run it with a profiling turned on, it takes only 3 seconds!
我没有找到有关此问题的任何信息.我唯一的猜测是,配置Parallel.ForEach
会比没有配置创建更多的线程,但是我不确定,即使是真的,我也不知道该怎么做.
I didn't find any information about this problem. My only guess is that with profiling Parallel.ForEach
creates more threads than without it, but I don't know for sure, and even if it's true, I don't know what to do with it.
那么,为什么会发生这种情况?在不进行概要分析的情况下运行应用程序时如何归档这种性能?
So, why is that happens and how can I archieve this performance when I run the application without profiling?
UPD. Parallel
与此无关:我使用简单的foreach
进行了测试,并且操作仍在3秒内完成!
UPD. Parallel
has nothing to do with this: I tested with simple foreach
instead and the operation still completes in 3 seconds!
推荐答案
我找到了答案:
如果尝试单独运行.exe或通过以下方式运行程序 使用"Debug> Start Without Debugging"(或仅按Ctrl + F5)打开IDE 该应用程序应与分析器一样快地运行.
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
我没有早点找到它,因为我认为原因是并发的.
I didn't find it earlier because I thought that the reason is concurrency.
这篇关于通过Visual Studio性能分析,应用程序运行更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!