静态链接VS动态链接 [英] Static linking vs dynamic linking

查看:130
本文介绍了静态链接VS动态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么令人信服的性能的原因选择了在某些情况下动态链接或反之亦然静态链接?我听说过或阅读以下内容,但我不知道够不上受到保证其真实性。

Are there any compelling performance reasons to choose static linking over dynamic linking or visa versa in certain situations? I've heard or read the following, but I don't know enough on the subject to vouch for their veracity.

1)静态链接和动态链接之间的性能的区别通常是可以忽略不计。

1) The difference in performance between static linking and dynamic linking is usually negligible.

2)(1)如果用一个使用配置文件数据来优化程序hotpaths一个分析,因为编译器与静态链接,编译器可以同时优化您的code和库code是不正确的。有了动态链接只有你的code可以优化。如果大部分的时间都花在运行库code,这可以使一个很大的区别。否则,(1)仍然适用。

2) (1) is not true if using a profiling compiler that uses profile data to optimize program hotpaths because with static linking, the compiler can optimize both your code and the library code. With dynamic linking only your code can be optimized. If most of the time is spent running library code, this can make a big difference. Otherwise, (1) still applies.

推荐答案


  • 动态链接可以减少资源消耗总量(如果有多个进程共享相同的库(包括版本中的同,当然))。我相信这是驱动它的presence在大多数环境中的说法。这里的资源包括磁盘空间,RAM和高速缓存空间。当然,如果你的动态连接器是不够灵活,有 DLL地狱的风险。

  • 动态链接意味着bug修复和升级库传播以改善的的产品,而不需要运送任何东西。

  • 插件始终呼吁的动态链接。

  • 静态链接,意味着你可以知道code将运行非常的有限的环境(早在启动过程中,还是在救援模式)。

  • 静态链接可以让双星易于分发以不同用户环境(在发送大而多,耗资源的程序的费用)。

  • 静态链接可能允许稍微的更快的启动次,但是这取决于在一定程度上对你的程序的大小和复杂性的的上在操作系统加载策略的细节。

  • Dynamic linking can reduce total resource consumption (if more than one process shares the same library (including the version in "the same", of course)). I believe this is the argument that drives it its presence in most environments. Here "resources" includes disk space, RAM, and cache space. Of course, if your dynamic linker is insufficiently flexible there is a risk of DLL hell.
  • Dynamic linking means that bug fixes and upgrades to libraries propagate to improve your product without requiring you to ship anything.
  • Plugins always call for dynamic linking.
  • Static linking, means that you can know the code will run in very limited environments (early in the boot process, or in rescue mode).
  • Static linking can make binaries easier to distribute to diverse user environments (at the cost of sending a large and more resource hungry program).
  • Static linking may allow slightly faster startup times, but this depends to some degree on both the size and complexity of your program and on the details of the OSs loading strategy.
  • 有些修改,包括评价和在其他的答案非常相关的建议。我想指出,你对这个突破的方法取决于你计划在运行什么样的环境有很多。最小的嵌入式系统可能没有足够的资源来支持动态链接。稍大的小系统可以很好地支持链接,因为它们的内存是足够小,从动态链接非常有吸引力使RAM节约。完全成熟的消费个人电脑都有,马克指出,大量的资源,你可能可以让方便的问题驱使你在这个问题上的想法。

    Some edits to include the very relevant suggestions in the comments and in other answers. I'd like to note that the way you break on this depends a lot on what environment you plan to run in. Minimal embedded systems may not have enough resources to support dynamic linking. Slightly larger small systems may well support linking, because their memory is small enough to make the RAM savings from dynamic linking very attractive. Full blown consumer PCs have, as Mark notes, enormous resources, and you can probably let the convenience issues drive you thinking on this matter.

    要解决性能和效率问题:这取决于

    To address the performance and efficiency issues: it depends.

    经典,动态库需要某种胶层这往往意味着双重分派或间接的寻址功能额外的一层,并可以花费一点速度(但函数调用时实际上是你的运行时间的一个重要组成部分? ?)。

    Classically, dynamic libraries require a some kind of glue layer which often means double dispatch or an extra layer of indirection in function addressing and can cost a little speed (but is function calling time actually a big part of your running time???).

    不过,如果你正在运行多个进程,所有调用同一个库中的很多,你可以为此节省的高速缓存行使用动态链接相对使用静态链接时(从而赢得上运行的性能)。 (除非现代操作系统的是足够聪明,在静态链接二进制文件相同的段,似乎很难,谁知道?)

    However, if you are running multiple processes which all call the same library a lot, you can end up saving cache lines (and thus winning on running performance) when using dynamic linking relative using static linking. (Unless modern OS's are smart enough to notice identical segments in statically linked binaries. Seems hard, anyone know?)

    另一个问题:加载时间。你付出装载在某些时候的成本。当你付出这个成本取决于OS是如何工作的,以及哪些连接使用。也许你宁愿推迟付款,直到你知道你需要它。

    Another issue: loading time. You pay loading costs at some point. When you pay this cost depends on how the OS works as well as what linking you use. Maybe you'd rather put off paying it until you know you need it.

    需要注意的是静态VS - 动态链接是传统的的一个优化的问题,因为它们都涉及到单独的编译到目标文件。然而,这不是必需的:一个编译器可以在原则上,通过将这些的AST为主要code产生的那些,从而赋予编译静态库发送给消化AST形式最初,和链接它们全局优化。我用做系统的无,所以我不能上以及它如何运作发表评论。

    Note that static-vs--dynamic linking is traditionally not a optimization issue, because they both involve separate compilation down to object files. However, this is not required: a compiler can in principle, "compile" "static libraries" to a digested AST form initially, and "link" them by adding those ASTs to the ones generated for the main code, thus empowering global optimization. None of the systems I use do this, so I can't comment on how well it works.

    要回答的性能问题的方法是的总是的通过测试(并尽可能多使用一个测试环境,如部署环境越好)。

    The way to answer performance questions is always by testing (and use an test environment as much like the deployment environment as possible).

    这篇关于静态链接VS动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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