- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个纯抽象基类和两个派生类:
struct B { virtual void foo() = 0; };
struct D1 : B { void foo() override { cout << "D1::foo()" << endl; } };
struct D2 : B { void foo() override { cout << "D1::foo()" << endl; } };
在 A 点调用 foo
与调用非虚拟成员函数的成本相同吗?或者它是否比 D1 和 D2 不是从 B 派生的更昂贵?
int main() {
D1 d1; D2 d2;
std::vector<B*> v = { &d1, &d2 };
d1.foo(); d2.foo(); // Point A (polymorphism not necessary)
for(auto&& i : v) i->foo(); // Polymorphism necessary.
return 0;
}
答案: Andy Prowl 的答案是正确的答案,我只是想添加 gcc 的汇编输出(在 godbolt 中测试:gcc- 4.7 -O2 -march=native -std=c++11)。直接函数调用的成本是:
mov rdi, rsp
call D1::foo()
mov rdi, rbp
call D2::foo()
对于多态调用:
mov rdi, QWORD PTR [rbx]
mov rax, QWORD PTR [rdi]
call [QWORD PTR [rax]]
mov rdi, QWORD PTR [rbx+8]
mov rax, QWORD PTR [rdi]
call [QWORD PTR [rax]]
但是,如果对象不是从 B
派生的,而您只是执行直接调用,gcc 将内联函数调用:
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:std::cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
如果 D1
和 D2
不是从 B
派生的,这可以实现进一步的优化所以我猜不,它们不等价(至少对于具有这些优化的 gcc 版本,-O3 产生了类似的输出而没有内联)。在 D1
和 D2
确实派生自 B
的情况下,是否存在阻止编译器内联的内容?
“修复”:使用委托(delegate)(也就是自己重新实现虚函数):
struct DG { // Delegate
std::function<void(void)> foo;
template<class C> DG(C&& c) { foo = [&](void){c.foo();}; }
};
然后创建一个委托(delegate) vector :
std::vector<DG> v = { d1, d2 };
如果您以非多态方式访问方法,则允许内联。但是,我猜访问 vector 会比仅使用虚函数(还不能用 godbolt 测试)慢(或者至少一样快,因为 std::function
使用虚函数进行类型删除)。
最佳答案
Does calling foo in Point A cost the same as a call to a non-virtual member function?
是的。
Or is it more expensive than if D1 and D2 wouldn't have derived from B?
没有。
编译器将静态解析这些函数调用,因为它们不是通过指针或引用执行的。由于调用该函数的对象的类型在编译时是已知的,因此编译器知道必须调用 foo()
的哪个实现。
关于c++ - 以非多态方式调用虚函数的成本是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14922890/
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
声明引用会导致运行时成本吗? Typename a; auto& b=a; func(b); 在循环内声明引用会导致多倍的运行时成本吗? Typename a=Typename();//defa
给定一组代表(成本, yield )的样本数据 items = [ (1000, 300), (500, 150), (400, 120), (300, 100), (200, 50), (55, 2
我从 link 得到这个其中谈到了外部归并排序。 来自幻灯片 6 示例:使用 5 个缓冲页,对 108 页文件进行排序 第 0 次:[108/5] = 22 次排序运行,每次运行 5 页(最后一次运行
使用 Javascript 在 localStorage 中查找值的速度有多快? 有没有人有指向任何性能测试的链接,这些测试表明是否值得在 JavaScript 对象中缓存数据?或者浏览器是否已经缓存
我正在尝试创建一个电子表格,以跟踪具有已知保质期的元素的当前和 future 成本。这包括产品是什么、产品成本、产品生命周期(以月为单位)和最后购买日期。 我已经尝试了几种方法来摆弄 Excel 公式
我正在使用最佳匹配算法在 TraMineR 中进行序列分析。不幸的是,我的 由于右删失数据,序列长度不等 .我的序列的最小长度是 5,最大长度是 11。长度的变化对于我感兴趣的序列之间的差异没有意义。
我读过一些文章说你应该将成本设置为至少 16 (216),但其他人说 8 左右就可以了。 是否有任何官方标准应该将成本设置为多高? 最佳答案 您应该使用的成本取决于您的硬件(和实现)的速度。 一般来说
我记得在我的架构类中假设L1缓存命中为1个周期(即与寄存器访问时间相同),但是在现代x86处理器上实际上是真的吗? L1缓存命中需要几个周期?与注册访问权限相比如何? 最佳答案 这是一篇很棒的文章:
我正在尝试确定来自托管我的 azure 函数的 azure 存储帐户的成本。我主要在本地进行开发,并使用 azure 存储模拟器并运行 func start cmd。我的问题是,此设置是否仍然会增加我
我有一个为工作编写的大型复杂 VBA 脚本。我正在清理它,并注意到我可以用比我所做的更动态的方式定义我的数组。 最初我将数组定义为字符串,如下所示: Dim header_arr(6) As Stri
任何人都可以为我指定以下情况下的费用: 当使用快照监听器的查询监听集合并且集合中的一个文档将被添加或更新时,我是否需要为已更新的文档或查询中的所有文档付费? 示例:我在用户集合上有一个快照监听器,其中
摘要 我正在使用 Octave 和 Ling-Spam 语料库构建垃圾邮件与普通邮件的分类器;我的分类方法是逻辑回归。 较高的学习率会导致计算成本为 NaN 值,但它不会破坏/降低分类器本身的性能。
我正在从事一个项目,其中我的代码的吞吐量非常重要,经过一番考虑后我选择让我的程序线程化。 主线程和子线程都在两个共享字典中添加和删除。考虑到在 python 中锁定的性能,我一直在通过互联网查看一些输
所以我在 TCP 套接字上发送数据,以数据大小为前缀,如下所示: write(socket, &length, sizeof(length)); write(socket, data, length)
我正在评估 Azure 媒体服务作为我们正在构建的解决方案的托管平台。我已成功使用 DRM 设置动态加密并使用 Azure AD 设置内容保护。我还检查了定价,我知道您必须为编码作业(一次性)、流媒体
AWS S3 Java SDK 提供了一种方法 doesObjectExist()检查 S3 中是否存在对象。它内部使用什么操作?是吗GET , LIST , 或 HEAD ? 我的担忧主要与它的成本
我一直在使用 three.js 来试验和学习 GLSL 和 WebGL。我来自 3d 艺术世界,所以我了解网格、3d 数学、照明等的概念。虽然我确实查阅了 OpenGL 和 WebGL 文献(以及 g
我正在 Azure 中设计一个 Web 服务。是否可以计量每个最终用户的实际 Azure 平台使用成本? Azure 是否向最终用户提供计费服务? 最佳答案 如今的 Windows Azure 计费模
我目前在 MySql 中有一个表,如果我运行此查询,则有 730 万行,大小为 1.5GB: How to get the sizes of the tables of a mysql databas
我是一名优秀的程序员,十分优秀!