問題描述
我從很多人那里聽說使用模板會(huì)使代碼變慢.真的嗎.我目前正在建造一個(gè)圖書館.有些地方如果不創(chuàng)建模板,會(huì)導(dǎo)致代碼管理問題.到目前為止,我可以想到兩個(gè)解決此問題的方法:
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
使用模板并在頭文件/庫本身中定義所有可能的類型,但不允許最終用戶創(chuàng)建模板實(shí)例.
Use templates and define all possible types in the header file/library itself but do not allow end user to make template instances.
例如typedef Graph
等
無論如何,是否可以限制用戶自己創(chuàng)建各種模板實(shí)例.
Is there anyway, to restrict user from creating various template instances on their own.
對上述查詢的幫助將受到高度重視.
Help on above queries would be highly regarded.
推薦答案
簡短的回答是否定的.如需更長的答案,請繼續(xù)閱讀.
正如其他人已經(jīng)指出的那樣,模板沒有直接的運(yùn)行時(shí)懲罰——即它們的所有技巧都發(fā)生在編譯時(shí).然而,在某些情況下,它們可以間接地減慢速度.特別是,模板的每個(gè)實(shí)例化(通常)產(chǎn)生的代碼與其他實(shí)例化是分開和唯一的.在少數(shù)情況下,這可能會(huì)導(dǎo)致執(zhí)行緩慢,因?yàn)樗皇巧勺銐蚨嗟哪繕?biāo)代碼,使其不再適合緩存.
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.
關(guān)于代碼大小:是的,大多數(shù)編譯器可以并且會(huì)為相同實(shí)例化的代碼折疊在一起——但是通常只有當(dāng)實(shí)例化是真實(shí)的時(shí)才會(huì)出現(xiàn)這種情況完全相同的.編譯器不會(huì)不插入代碼來進(jìn)行最微不足道的轉(zhuǎn)換,以獲得兩個(gè)細(xì)微不同的實(shí)例來相互匹配.例如,一個(gè)普通的函數(shù)調(diào)用可以并且將把 T *
轉(zhuǎn)換為 T const *
所以調(diào)用使用 const
或非 const
參數(shù)將使用相同的代碼(除非您選擇在 const
ness 上重載函數(shù),在這種情況下,您可能已經(jīng)專門為這兩種情況提供不同的行為).使用模板則不會(huì)發(fā)生這種情況——對 T *
和 T const *
的實(shí)例化將導(dǎo)致生成兩個(gè)完全獨(dú)立的代碼段.可能編譯器(或鏈接器)事后能夠合并兩者,但并不完全確定(例如,我肯定使用過沒有的編譯器).
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 const
ness, 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).
但最終,模板對速度的積極影響遠(yuǎn)多于消極影響.
But in the end, templates have positive effects on speed far more often than negative.
這篇關(guān)于C++ 模板會(huì)使程序變慢嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!