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

在 C++ 類中使用虛方法的性能成本是多少?

What is the performance cost of having a virtual method in a C++ class?(在 C++ 類中使用虛方法的性能成本是多少?)
本文介紹了在 C++ 類中使用虛方法的性能成本是多少?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 C++ 類(或其任何父類)中至少有一個虛擬方法意味著該類將有一個虛擬表,并且每個實例都有一個虛擬指針.

Having at least one virtual method in a C++ class (or any of its parent classes) means that the class will have a virtual table, and every instance will have a virtual pointer.

所以內存開銷就很清楚了.最重要的是實例的內存成本(特別是如果實例很小,例如如果它們只是打算包含一個整數:在這種情況下,在每個實例中都有一個虛擬指針可能會使實例的大小增加一倍.至于虛擬表使用的內存空間,我想與實際方法代碼使用的空間相比,通常可以忽略不計.

So the memory cost is quite clear. The most important is the memory cost on the instances (especially if the instances are small, for example if they are just meant to contain an integer: in this case having a virtual pointer in every instance might double the size of the instances. As for the memory space used up by the virtual tables, I guess it is usually negligible compared to the space used up by the actual method code.

這讓我想到了一個問題:使方法虛擬化是否存在可衡量的性能成本(即速度影響)?每次調用方法時都會在運行時在虛擬表中進行查找,因此如果對此方法的調用非常頻繁,并且如果此方法很短,那么可能會出現可衡量的性能下降?我想這取決于平臺,但有人運行過一些基準測試嗎?

This brings me to my question: is there a measurable performance cost (i.e. speed impact) for making a method virtual? There will be a lookup in the virtual table at runtime, upon every method call, so if there are very frequent calls to this method, and if this method is very short, then there might be a measurable performance hit? I guess it depends on the platform, but has anyone run some benchmarks?

我問這個問題的原因是我遇到了一個錯誤,該錯誤是由于程序員忘記定義 virtual 方法所致.這不是我第一次看到這種錯誤.我想:為什么我們在需要時添加 virtual 關鍵字而不是刪除 virtual 關鍵字,而我們絕對確定它不需要?如果性能成本低,我想我會在我的團隊中簡單地推薦以下內容:在每個類中將every方法默認設為虛擬,包括析構函數,并且僅在需要時將其刪除.你覺得這很瘋狂嗎?

The reason I am asking is that I came across a bug that happened to be due to a programmer forgetting to define a method virtual. This is not the first time I see this kind of mistake. And I thought: why do we add the virtual keyword when needed instead of removing the virtual keyword when we are absolutely sure that it is not needed? If the performance cost is low, I think I will simply recommend the following in my team: simply make every method virtual by default, including the destructor, in every class, and only remove it when you need to. Does that sound crazy to you?

推薦答案

I 在 3ghz 有序 PowerPC 處理器上運行一些計時.在該架構上,虛擬函數調用比直接(非虛擬)函數調用多花費 7 納秒.

I ran some timings on a 3ghz in-order PowerPC processor. On that architecture, a virtual function call costs 7 nanoseconds longer than a direct (non-virtual) function call.

因此,除非函數類似于簡單的 Get()/Set() 訪問器,否則不值得擔心成本,其中除內聯之外的任何東西都有些浪費.內聯到 0.5ns 的函數的 7ns 開銷是嚴重的;一個需要 500 毫秒來執行的函數的 7 納秒開銷是沒有意義的.

So, not really worth worrying about the cost unless the function is something like a trivial Get()/Set() accessor, in which anything other than inline is kind of wasteful. A 7ns overhead on a function that inlines to 0.5ns is severe; a 7ns overhead on a function that takes 500ms to execute is meaningless.

虛函數的巨大成本實際上并不是在 vtable 中查找函數指針(通常只是一個循環),而是間接跳轉通常無法進行分支預測.這可能會導致大的流水線氣泡,因為在間接跳轉(通過函數指針的調用)退出并計算新的指令指針之前,處理器無法獲取任何指令.因此,虛函數調用的成本比從程序集看起來要大得多……但仍然只有 7 納秒.

The big cost of virtual functions isn't really the lookup of a function pointer in the vtable (that's usually just a single cycle), but that the indirect jump usually cannot be branch-predicted. This can cause a large pipeline bubble as the processor cannot fetch any instructions until the indirect jump (the call through the function pointer) has retired and a new instruction pointer computed. So, the cost of a virtual function call is much bigger than it might seem from looking at the assembly... but still only 7 nanoseconds.

Andrew、不確定和其他人也提出了一個很好的觀點,即虛函數調用可能導致指令緩存未命中:如果跳轉到不在緩存中的代碼地址,那么當指令從主存儲器中取出時,整個程序就停止了.這總是一個明顯的停頓:在氙氣上,大約 650 個周期(根據我的測試).

Andrew, Not Sure, and others also raise the very good point that a virtual function call may cause an instruction cache miss: if you jump to a code address that is not in cache then the whole program comes to a dead halt while the instructions are fetched from main memory. This is always a significant stall: on Xenon, about 650 cycles (by my tests).

然而,這不是虛函數特有的問題,因為如果跳轉到不在緩存中的指令,即使是直接的函數調用也會導致未命中.重要的是該函數是否最近運行過(使其更有可能在緩存中),以及您的架構是否可以預測靜態(非虛擬)分支并提前將這些指令提取到緩存中.我的 PPC 沒有,但也許英特爾最新的硬件有.

However this isn't a problem specific to virtual functions because even a direct function call will cause a miss if you jump to instructions that aren't in cache. What matters is whether the function has been run before recently (making it more likely to be in cache), and whether your architecture can predict static (not virtual) branches and fetch those instructions into cache ahead of time. My PPC does not, but maybe Intel's most recent hardware does.

我的時間控制了 icache 未命中對執行的影響(故意的,因為我試圖孤立地檢查 CPU 管道),所以他們打折了這個成本.

My timings control for the influence of icache misses on execution (deliberately, since I was trying to examine the CPU pipeline in isolation), so they discount that cost.

這篇關于在 C++ 類中使用虛方法的性能成本是多少?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 成都思迪机电技术研究所-四川成都思迪编码器 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 莱州网络公司|莱州网站建设|莱州网站优化|莱州阿里巴巴-莱州唯佳网络科技有限公司 | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 橡胶接头_橡胶软接头_套管伸缩器_管道伸缩器厂家-巩义市远大供水材料有限公司 | 空压机网_《压缩机》杂志 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 杭州双螺杆挤出机-百科| 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 沙盘模型公司_沙盘模型制作公司_建筑模型公司_工业机械模型制作厂家 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 医用空气消毒机-医用管路消毒机-工作服消毒柜-成都三康王 | 游泳池设备安装工程_恒温泳池设备_儿童游泳池设备厂家_游泳池水处理设备-东莞市君达泳池设备有限公司 | 杭州实验室尾气处理_实验台_实验室家具_杭州秋叶实验设备有限公司 | 南京技嘉环保科技有限公司-杀菌除臭剂|污水|垃圾|厕所|橡胶厂|化工厂|铸造厂除臭剂 | 福建珂朗雅装饰材料有限公司「官方网站」 | 电缆隧道在线监测-智慧配电站房-升压站在线监测-江苏久创电气科技有限公司 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 防爆暖风机_防爆电暖器_防爆电暖风机_防爆电热油汀_南阳市中通智能科技集团有限公司 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | 超声波清洗机_细胞破碎仪_实验室超声仪器_恒温水浴-广东洁盟深那仪器 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 电动葫芦|防爆钢丝绳电动葫芦|手拉葫芦-保定大力起重葫芦有限公司 | 潍坊大集网-潍坊信息港-潍坊信息网 | 超声波焊接机_超音波熔接机_超声波塑焊机十大品牌_塑料超声波焊接设备厂家 | MOOG伺服阀维修,ATOS比例流量阀维修,伺服阀维修-上海纽顿液压设备有限公司 | 考勤系统_考勤管理系统_网络考勤软件_政企|集团|工厂复杂考勤工时统计排班管理系统_天时考勤 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 天津暖气片厂家_钢制散热器_天津铜铝复合暖气片_维尼罗散热器 | 全自动定氮仪-半自动凯氏定氮仪厂家-祎鸿仪器 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 集装箱展厅-住人集装箱住宿|建筑|房屋|集装箱售楼处-山东锐嘉科技工程有限公司 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 |