在Visual Studio外部启动时,程序运行速度较慢 [英] Program runs slower when launched outside of Visual Studio

查看:157
本文介绍了在Visual Studio外部启动时,程序运行速度较慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我的程序有些奇怪的行为。我使用Visual Studio Professional 2013 Update 1在C ++中编写它,它由一个exe应用程序链接到多个DLL和调用这些DLL中定义的函数。



在我的主程序(由几千行代码组成)中,我调用一个DLL函数(让我们称之为 DLLFunction code>),并计算该调用所花费的时间,如下所示:

  auto beginTime = std :: chrono :: high_resolution_clock :: now(); 

DllFunction();

auto endTime = std :: chrono :: high_resolution_clock :: now();

long long totalTime = std :: chrono :: duration_cast< std :: chrono :: milliseconds>(endTime - startTime).count();

我注意到的是,在Visual Studio之外启动它需要更长的时间。例如:



在Visual Studio中使用调试器在调试中运行它 - > 〜50 ms



在Visual Studio 中使用调试器发布中运行 $ c>〜25 ms



在Visual Studio strong> ---> 〜20 ms



运行 release build)---> 〜80 ms



在Visual Studio之外的版本中运行它实际上比运行带有调试器的调试版本花费更长的时间!



令人讨厌的DLL由相同的编译器构建在同一个解决方案中,我已经双重检查所有的目录中的DLL,我从哪里启动我的应用程序是正确的。



这样的行为的原因是什么? p>

EDIT 5
主应用程序生成另一个控制台应用程序,并使用命名管道与其通信。原来,没有产生另一个应用程序使Visual Studio之外的DLL调用快速。



但是同样的应用程序在Visual Studio内部和外部产生,



编辑4
原来,这种缓慢的行为出现了只有如果我把函数调用放在我的主程序的代码的某些部分,所以它一定是一个与此相关的问题。这是很多行,但我会继续研究。



感谢您的建议,他们有助于识别问题。



/ strong>:
使用QueryPerfomanceCounter进行测量:



Visual Studio中测量的CPU周期(约50k)



按照频率除以频率显示与std :: chrono类似的结果。



EDIT 2
我使用进程管理器检查过的VS和VS外部的DLL是相同的。



编辑1 :根据要求,我尝试过:

  beginTime = std :: chrono :: high_resolution_clock :: now(); 

for(int i = 0; i <1000; ++ i)
{
DllFunction();
}
auto endTime = std :: chrono :: high_resolution_clock :: now();

long long totalTime = std :: chrono :: duration_cast< std :: chrono :: milliseconds>(endTime - startTime).count();

结果:
在Visual中运行 release 工作室附带调试器 ---> 〜19秒



运行 以外的Visual Studio( release build)---> 〜40秒

解决方案

DLL不会加载到程序中,直到你第一次调用它。如果该函数非常小,程序可能花费大部分时间只是加载DLL。

尝试更改为:

  DllFunction(); 
auto beginTime = std :: chrono :: high_resolution_clock :: now();

for(int i = 0; i <1000; ++ i)
{
DllFunction();
}
auto endTime = std :: chrono :: high_resolution_clock :: now();

long long totalTime = std :: chrono :: duration_cast< std :: chrono :: milliseconds>(endTime - startTime).count();

这样,不考虑加载时间。


I'm noticing some strange behaviour regarding my program. I'm writing it in C++ using Visual Studio Professional 2013 Update 1 and it consists of an exe application that links against multiple DLLs and calls functions that are defined in those DLLs.

In my main program (which consists of several thousands of lines of code) I call a DLL function (let's call it DLLFunction() ) and I calculate the time taken by that call, like this:

auto beginTime = std::chrono::high_resolution_clock::now();

DllFunction();

auto endTime = std::chrono::high_resolution_clock::now();

long long totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();

What I am noticing is that it takes a much longer time when launching it outside Visual Studio. For example:

Running it in debug within Visual Studio with debugger attached --> ~50 ms

Running it in release within Visual Studio with debugger attached ---> ~25 ms

Running it in release within Visual Studio without debugger attached ---> ~20 ms

Running it outside of Visual Studio (release build) ---> ~80 ms

As you can see running it in release outside of Visual Studio actually takes longer than running a debug build with a debugger attached!

The offending DLL is built within the same solution by the same compiler, and I've double checked that all DLLs in the directory from where I launch my application are the right ones.

What could be the reason of such a behaviour?

EDIT 5: The main application spawns another console application and communicates with it using named pipes. It turned out that not spawning that another application makes the DLL call fast outside Visual Studio.

However the same application is spawned both inside and outside Visual Studio, so i don't see why it slows down other calls just outside Visual Studio.

EDIT 4: It turned out that this slow behaviour appears only if i place the function call in some part of the code of my main program, so it must be a problem related to that. It's many lines, but i'll continue researching.

Thank you for suggestions anyway, they were useful to identificate the problem.

EDIT 3: Measurements with QueryPerfomanceCounter:

The CPU cycles measured inside Visual Studio (~50k) are half of those outside (~110k) (are those returned by QueryPerfomanceCounter() actual CPU cycles by the way?).

Dividing it by the frequency shows similar results to the std::chrono ones.

EDIT 2: I checked with process explorer as suggested, the DLLs loaded within VS and outside VS are identical.

EDIT 1: as requested, i tried this:

auto beginTime = std::chrono::high_resolution_clock::now();

for (int i = 0; i < 1000; ++i)
{
    DllFunction();
}
auto endTime = std::chrono::high_resolution_clock::now();

long long totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();

And the results: Running it in release within Visual Studio with debugger attached ---> ~19 seconds

Running it outside of Visual Studio (release build) ---> ~40 seconds

解决方案

The DLL isn't loaded in the program until you call it the first time. If that function is very small, the program could be spending the majority of it's time just loading the DLL.
Try changing to this:

DllFunction();
auto beginTime = std::chrono::high_resolution_clock::now();

for (int i = 0; i < 1000; ++i)
{
    DllFunction();
}
auto endTime = std::chrono::high_resolution_clock::now();

long long totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();

This way, the loading time isn't considered.

这篇关于在Visual Studio外部启动时,程序运行速度较慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆