简体   繁体   English

DLL是否比静态链接慢?

[英]Is a DLL slower than a static link?

I made a GUI library for games. 我为游戏制作了一个GUI库。 My test demo runs at 60 fps. 我的测试演示以60 fps运行。 When I run this demo with the static version of my library it takes 2-3% cpu in taskmanager. 当我使用我的库的静态版本运行此演示时,在taskmanager中需要2-3%的CPU。 When I use the DLL version it uses around 13-15%. 当我使用DLL版本时,它使用大约13-15%。 Is that normal? 这是正常的吗? Is so, how could I optimize it? 是这样,我怎么能优化它? I already ask it to use /O2 for the most function inlining. 我已经要求它使用/ O2进行大多数功能内联。

Do not start your performance timer until the DLL has had opportunity to execute its functionality one time. 在DLL有机会执行一次其功能之前,请不要启动性能计时器。 This gives it time to load into memory. 这使它有时间加载到内存中。 Then start the timer and check performance. 然后启动计时器并检查性能。 It should then basically match that of the static lib. 然后它应该基本上匹配静态lib的那个。

Also keep in mind that the load-location of the DLL can greatly affect how quickly it loads. 还要记住,DLL的加载位置会极大地影响它加载的速度。 The default base addres for DLLs is 0x400000. DLL的默认基本地址是0x400000。 If you already have some other DLL in that location, then the load process must perform an expensive re-addressing step which will throw off your timing even more. 如果您在该位置已经有其他DLL,那么加载过程必须执行一个昂贵的重新寻址步骤,这将使您的时间更加摒弃。

If you have such a conflict, just choose a different base address in Visual Studio. 如果您有这样的冲突,只需在Visual Studio中选择不同的基址。

You will have the overhead of loading the DLL (should be just once at the beginning). 您将有加载DLL的开销(应该只在开始时一次)。 It isn't statically linked in with direct calls, so I would expect a small amount of overhead but not much. 它与直接调用没有静态链接,因此我预计会有少量开销,但不会太多。

However, some DLLs will have much higher overheads. 但是,某些DLL将具有更高的开销。 I'm thinking of COM objects although there may be other examples. 我正在考虑COM对象,尽管可能还有其他例子。 COM adds a lot of overhead on function calls between objects. COM在对象之间的函数调用上增加了很多开销。

If you call DLL-functions they cannot be inlined for a caller. 如果调用DLL函数,则无法为调用者内联它们。 You should think a little about your DLL-boundaries. 你应该考虑一下你的DLL边界。

May be it is better for your application to have a small bootstrap exe which just executes a main loop in your DLL. 可能是你的应用程序有一个小的bootstrap exe,它只是在你的DLL中执行一个主循环。 This way you can avoid much overhead for function calls. 这样就可以避免函数调用的大量开销。

It's a little unclear as to what's being statically/dynamically linked. 关于什么是静态/动态链接,有点不清楚。 Is the DLL of your lib statically linked with its dependencies? 您的lib的DLL是否与其依赖项静态链接? Is it possible that the DLL is calling other DLLs (that will be slow)? DLL是否可能正在调用其他DLL(这会很慢)? Maybe try running a profiler from valgrind on your executable to determine where all the CPU usage is coming from. 也许尝试在可执行文件上运行valgrind的探查器,以确定所有CPU使用的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM