久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 模板會(huì)使程序變慢嗎?

Do c++ templates make programs slow?(C++ 模板會(huì)使程序變慢嗎?)
本文介紹了C++ 模板會(huì)使程序變慢嗎?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號..

我從很多人那里聽說使用模板會(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 GraphGraphI32;

無論如何,是否可以限制用戶自己創(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ù)將使用相同的代碼(除非您選擇在 constness 上重載函數(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 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).

但最終,模板對速度的積極影響遠(yuǎn)多于消極影響.

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

這篇關(guān)于C++ 模板會(huì)使程序變慢嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 日韩欧美天堂 | 亚洲一区二区三区四区五区午夜 | www.久| 99reav| 操久久| 日韩在线视频一区二区三区 | 日韩av一区二区在线 | 欧美一级大片 | av一级久久 | 久久久久久久久久久久久久av | 99亚洲精品 | 日韩一二三区视频 | 国产激情精品视频 | 欧美视频一区二区三区 | 成人在线视频网站 | 国产精品一区二区视频 | 福利精品在线观看 | 日韩成人av在线 | 三级视频在线观看 | 国产伦精品一区二区三区精品视频 | 久久久久久久久久久久91 | 亚洲视频手机在线 | 在线观看成年人视频 | 九九综合九九 | 精品一区二区三区四区视频 | 一区二区三区影院 | caoporn免费 | 免费在线观看黄网站 | 97人人超碰 | 国产一区二区欧美 | 不卡欧美 | 精品日韩 | 九九亚洲精品 | 这里有精品 | 亚洲国产精品久久久久 | aaa国产大片 | 国产成人免费视频网站高清观看视频 | 日韩精品视频网 | 国产中文字幕在线 | 国产精品777一区二区 | 成人精品毛片国产亚洲av十九禁 |