C++ 模板会使程序变慢吗? [英] Do c++ templates make programs slow?

查看:34
本文介绍了C++ 模板会使程序变慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从很多人那里听说使用模板会使代码变慢.真的吗.我目前正在建造一个图书馆.有些地方如果不创建模板,会导致代码管理问题.到目前为止,我可以想到两个解决此问题的方法:

I have heard from many people that usage of templates make the code slow. Is it really true. I'm currently building a library. There are places where if templates are not created, it would result in code management problem. As of now I can think two solutions to this problem:

  • 使用#defines

  • use #defines

使用模板并在头文件/库本身中定义所有可能的类型,但不允许最终用户创建模板实例.

Use templates and define all possible types in the header file/library itself but do not allow end user to make template instances.

例如typedef GraphGraphI32;

无论如何,是否可以限制用户自己创建各种模板实例.

Is there anyway, to restrict user from creating various template instances on their own.

对上述查询的帮助将受到高度重视.

Help on above queries would be highly regarded.

推荐答案

简短的回答是否定的.如需更长的答案,请继续阅读.

正如其他人已经指出的那样,模板没有直接的运行时惩罚——即它们的所有技巧都发生在编译时.然而,在某些情况下,它们可以间接地减慢速度.特别是,模板的每个实例化(通常)产生的代码与其他实例化是分开和唯一的.在少数情况下,这可能会导致执行缓慢,因为它只是生成足够多的目标代码,使其不再适合缓存.

As others have already noted, templates don't have a direct run-time penalty -- i.e. all their tricks happen at compile time. Indirectly, however, they can slow things down under a few circumstances. In particular, each instantiation of a template (normally) produces code that's separate and unique from other instantiations. Under a few circumstances, this can lead to slow execution, by simply producing enough object code that it no longer fits in the cache well.

关于代码大小:是的,大多数编译器可以并且会为相同实例化的代码折叠在一起——但是通常只有当实例化是真实的时才会出现这种情况完全相同的.编译器不会插入代码来进行最微不足道的转换,以获得两个细微不同的实例来相互匹配.例如,一个普通的函数调用可以并且将把 T * 转换为 T const * 所以调用使用 const 或非 const 参数将使用相同的代码(除非您选择在 constness 上重载函数,在这种情况下,您可能已经专门为这两种情况提供不同的行为).使用模板则不会发生这种情况——对 T *T const * 的实例化将导致生成两个完全独立的代码段.可能编译器(或链接器)事后能够合并两者,但并不完全确定(例如,我肯定使用过没有的编译器).

With respect to code size: yes, most compilers can and will fold together the code for identical instantiations -- but that's normally the case only when the instantiations are truly identical. The compiler will not insert code to do even the most trivial conversions to get two minutely different instantiations to match each other. For example, a normal function call can and will convert T * to T const * so calls that use either const or non-const arguments will use the same code (unless you've chosen to overload the function on constness, in which case you've probably done so specifically to provide different behavior for the two cases). With a template, that won't happen -- instantiations over T * and T const * will result in two entirely separate pieces of code being generated. It's possible the compiler (or linker) may be able to merge the two after the fact, but not entirely certain (e.g., I've certainly used compilers that didn't).

但最终,模板对速度的积极影响远多于消极影响.

But in the end, templates have positive effects on speed far more often than negative.

这篇关于C++ 模板会使程序变慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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