pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

RTTI有多貴?

How expensive is RTTI?(RTTI有多貴?)
本文介紹了RTTI有多貴?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我知道使用 RTTI 會影響資源,但影響有多大?我看過的所有地方都只是說RTTI 很貴",但實際上沒有一個提供任何基準(zhǔn)或定量數(shù)據(jù)來保護(hù)內(nèi)存、處理器時間或速度.

I understand that there is a resource hit from using RTTI, but how big is it? Everywhere I've looked just says that "RTTI is expensive," but none of them actually give any benchmarks or quantitative data reguarding memory, processor time, or speed.

那么,RTTI 有多貴?我可能會在只有 4MB 內(nèi)存的嵌入式系統(tǒng)上使用它,所以每一位都很重要.

So, just how expensive is RTTI? I might use it on an embedded system where I have only 4MB of RAM, so every bit counts.

根據(jù) S. Lott 的回答,這樣會更好如果我包括我實際在做的事情.我正在使用一個類來傳遞不同長度的數(shù)據(jù)并且可以執(zhí)行不同的操作,因此使用只有虛函數(shù).似乎使用幾個 dynamic_cast 可以解決這個問題,允許不同的派生類通過不同的級別,但仍然允許它們以完全不同的方式運行.

As per S. Lott's answer, it would be better if I include what I'm actually doing. I am using a class to pass in data of different lengths and that can perform different actions, so it would be difficult to do this using only virtual functions. It seems that using a few dynamic_casts could remedy this problem by allowing the different derived classes to be passed through the different levels yet still allow them to act completely differently.

根據(jù)我的理解,dynamic_cast 使用 RTTI,所以我想知道在有限的系統(tǒng)上使用它有多可行.

From my understanding, dynamic_cast uses RTTI, so I was wondering how feasable it would be to use on a limited system.

推薦答案

不管編譯器如何,只要你負(fù)擔(dān)得起,你總是可以節(jié)省運行時間

Regardless of compiler, you can always save on runtime if you can afford to do

if (typeid(a) == typeid(b)) {
  B* ba = static_cast<B*>(&a);
  etc;
}

代替

B* ba = dynamic_cast<B*>(&a);
if (ba) {
  etc;
}

前者只涉及std::type_info的一個比較;后者必然涉及遍歷繼承樹和比較.

The former involves only one comparison of std::type_info; the latter necessarily involves traversing an inheritance tree plus comparisons.

過去……就像大家說的那樣,資源使用是特定于實現(xiàn)的.

Past that ... like everyone says, the resource usage is implementation specific.

我同意其他人的意見,即提交者出于設(shè)計原因應(yīng)避免使用 RTTI.然而,使用 RTTI 有很好的理由(主要是因為 boost::any).請記住,了解其在常見實現(xiàn)中的實際資源使用情況很有用.

I agree with everyone else's comments that the submitter should avoid RTTI for design reasons. However, there are good reasons to use RTTI (mainly because of boost::any). That in mind, it's useful to know its actual resource usage in common implementations.

我最近對 ??GCC 中的 RTTI 進(jìn)行了大量研究.

I recently did a bunch of research into RTTI in GCC.

tl;dr: GCC 中的 RTTI 使用的空間可以忽略不計,并且 typeid(a) == typeid(b) 在許多平臺上(Linux、BSD 和嵌入式平臺,但不是 mingw32)非常快).如果您知道自己將永遠(yuǎn)在一個受祝福的平臺上,那么 RTTI 非常接近免費.

tl;dr: RTTI in GCC uses negligible space and typeid(a) == typeid(b) is very fast, on many platforms (Linux, BSD and maybe embedded platforms, but not mingw32). If you know you'll always be on a blessed platform, RTTI is very close to free.

重要細(xì)節(jié):

GCC 更喜歡使用特定的供應(yīng)商中立"C++ ABI[1],并且始終將此 ABI 用于 Linux 和 BSD 目標(biāo)[2].對于支持此 ABI 和弱鏈接的平臺,typeid() 為每種類型返回一致且唯一的對象,甚至跨越動態(tài)鏈接邊界.您可以測試 &typeid(a) == &typeid(b),或者僅依賴于便攜式測試 typeid(a) == typeid(b) 實際上只是在內(nèi)部比較一個指針.

GCC prefers to use a particular "vendor-neutral" C++ ABI[1], and always uses this ABI for Linux and BSD targets[2]. For platforms that support this ABI and also weak linkage, typeid() returns a consistent and unique object for each type, even across dynamic linking boundaries. You can test &typeid(a) == &typeid(b), or just rely on the fact that the portable test typeid(a) == typeid(b) does actually just compare a pointer internally.

在 GCC 的首選 ABI 中,類 vtable 總是 持有一個指向每個類型的 RTTI 結(jié)構(gòu)的指針,盡管它可能不被使用.因此,typeid() 調(diào)用自身應(yīng)該只花費與任何其他 vtable 查找(與調(diào)用虛擬成員函數(shù)相同)的成本,并且 RTTI 支持 應(yīng)該't 為每個對象使用任何額外的空間.

In GCC's preferred ABI, a class vtable always holds a pointer to a per-type RTTI structure, though it might not be used. So a typeid() call itself should only cost as much as any other vtable lookup (the same as calling a virtual member function), and RTTI support shouldn't use any extra space for each object.

據(jù)我所知,GCC 使用的 RTTI 結(jié)構(gòu)(這些都是 std::type_info 的子類)只為每種類型保存幾個字節(jié),除了名稱.即使使用 -fno-rtti,我也不清楚輸出代碼中是否存在名稱.無論哪種方式,編譯后的二進(jìn)制文件大小的變化都應(yīng)該反映運行時內(nèi)存使用的變化.

From what I can make out, the RTTI structures used by GCC (these are all the subclasses of std::type_info) only hold a few bytes for each type, aside from the name. It isn't clear to me whether the names are present in the output code even with -fno-rtti. Either way, the change in size of the compiled binary should reflect the change in runtime memory usage.

一個快速實驗(在 Ubuntu 10.04 64 位上使用 GCC 4.4.3)表明 -fno-rtti 實際上增加一個簡單測試程序的二進(jìn)制大小幾百字節(jié).這在 -g-O3 的組合中一致發(fā)生.我不確定為什么尺寸會增加;一種可能性是 GCC 的 STL 代碼在沒有 RTTI 的情況下表現(xiàn)不同(因為異常不起作用).

A quick experiment (using GCC 4.4.3 on Ubuntu 10.04 64-bit) shows that -fno-rtti actually increases the binary size of a simple test program by a few hundred bytes. This happens consistently across combinations of -g and -O3. I'm not sure why the size would increase; one possibility is that GCC's STL code behaves differently without RTTI (since exceptions won't work).

[1] 稱為 Itanium C++ ABI,記錄在 http://www.codesourcery.com/public/cxx-abi/abi.html.名稱非常混亂:名稱指的是原始開發(fā)架構(gòu),盡管 ABI 規(guī)范適用于許多架構(gòu),包括 i686/x86_64.GCC 內(nèi)部源代碼和 STL 代碼中的注釋將 Itanium 稱為新"ABI,與他們之前使用的舊"ABI 形成對比.更糟糕的是,新"/安騰 ABI 指的是通過 -fabi-version 可用的所有版本;舊" ABI 早于此版本控制.GCC 在 3.0 版本中采用了 Itanium/versioned/"new" ABI;如果我正確閱讀了他們的變更日志,則在 2.95 及更早版本中使用了舊"ABI.

[1] Known as the Itanium C++ ABI, documented at http://www.codesourcery.com/public/cxx-abi/abi.html. The names are horribly confusing: the name refers to the original development architecture, though the ABI specification works on lots of architectures including i686/x86_64. Comments in GCC's internal source and STL code refer to Itanium as the "new" ABI in contrast to the "old" one they used before. Worse, the "new"/Itanium ABI refers to all versions available through -fabi-version; the "old" ABI predated this versioning. GCC adopted the Itanium/versioned/"new" ABI in version 3.0; the "old" ABI was used in 2.95 and earlier, if I am reading their changelogs correctly.

[2] 我找不到按平臺列出std::type_info 對象穩(wěn)定性的任何資源.對于我可以訪問的編譯器,我使用了以下內(nèi)容:echo "#include "|gcc -E -dM -x c++ -c - |grep GXX_MERGED_TYPEINFO_NAMES.從 GCC 3.0 開始,此宏在 GCC 的 STL 中控制 operator== 對于 std::type_info 的行為.我確實發(fā)現(xiàn) mingw32-gcc 遵守 Windows C++ ABI,其中 std::type_info 對象對于跨 DLL 的類型不是唯一的;typeid(a) == typeid(b) 在幕后調(diào)用 strcmp.我推測在像 AVR 這樣沒有代碼鏈接的單程序嵌入式目標(biāo)上,std::type_info 對象總是穩(wěn)定的.

[2] I couldn't find any resource listing std::type_info object stability by platform. For compilers I had access to, I used the following: echo "#include <typeinfo>" | gcc -E -dM -x c++ -c - | grep GXX_MERGED_TYPEINFO_NAMES. This macro controls the behavior of operator== for std::type_info in GCC's STL, as of GCC 3.0. I did find that mingw32-gcc obeys the Windows C++ ABI, where std::type_info objects aren't unique for a type across DLLs; typeid(a) == typeid(b) calls strcmp under the covers. I speculate that on single-program embedded targets like AVR, where there is no code to link against, std::type_info objects are always stable.

這篇關(guān)于RTTI有多貴?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 合肥花魁情感婚姻咨询中心_挽回爱情_修复婚姻_恋爱指南 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 迪威娱乐|迪威娱乐客服|18183620002 | 瓶盖扭矩仪(扭力值检测)-百科| 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 施工围挡-施工PVC围挡-工程围挡-深圳市旭东钢构技术开发有限公司 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 塑料检查井_双扣聚氯乙烯增强管_双壁波纹管-河南中盈塑料制品有限公司 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 江苏密集柜_电动_手动_移动_盛隆柜业江苏档案密集柜厂家 | 免联考国际MBA_在职MBA报考条件/科目/排名-MBA信息网 | 不锈钢搅拌罐_高速搅拌罐厂家-无锡市凡格德化工装备科技有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 广州工业氧气-工业氩气-工业氮气-二氧化碳-广州市番禺区得力气体经营部 | 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 环球周刊网| 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 踏板力计,制动仪,非接触多功能速度仪,逆反射系数测试仪-创宇 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 德国进口电锅炉_商用电热水器_壁挂炉_电采暖器_电热锅炉[德国宝] | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 双杰天平-国产双杰电子天平-美国双杰-常熟双杰仪器 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 搪瓷搅拌器,搪玻璃搅拌器,搪玻璃冷凝器_厂家-淄博越宏化工设备 | 电动葫芦-河北悍象起重机械有限公司| 上海律师咨询_上海法律在线咨询免费_找对口律师上策法网-策法网 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 西安微信朋友圈广告投放_微信朋友圈推广_西安度娘网络科技有限公司 | 石家庄救护车出租_重症转院_跨省跨境医疗转送_活动赛事医疗保障_康复出院_放弃治疗_腾康26年医疗护送转诊团队 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 |