- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我知道虚函数本质上是包含在 vtable 中的函数指针,由于间接等原因,这会使多态调用变慢。但是我想知道调用是确定性的时编译器优化。我所说的确定性是指以下情况:
struct Foo
{
virtual void DoSomething(){....}
};
int main()
{
Foo myfoo;
myfoo.DoSemthing();
return 0;
}
struct Foo
{
virtual void DoSomething();
};
struct Bar : public Foo
{
virtual void DoSomething();
};
int main()
{
Foo* a = new Foo();
a->DoSomething(); //Overhead ? a doesn't seem to be able to change nature.
Foo* b = new Bar();
b->DoSomething(); //Overhead ? It's a polymorphic call, but b's nature is deterministic.
Bar* c = new Bar();
c->DoSomething(); //Overhead ? It is NOT possible to have an other version of the method than Bar::DoSomething
return 0;
}
最佳答案
在第一种情况下,这不是虚拟调用。编译器将直接调用 Foo::DoSomething()
。
第二种情况比较复杂。其一,它至多是链接时间优化,因为对于特定的翻译单元,编译器不知道还有谁可能继承自该类。您遇到的另一个问题是共享库,它们也可能在您的可执行文件对其一无所知的情况下继承。
不过,总的来说,这是一种称为虚拟函数调用消除或去虚拟化的编译器优化,并且在某种程度上是一个活跃的研究领域。一些编译器在某种程度上做到了这一点,而另一些则根本不这样做。
参见,在 GCC (g++) 中,-fdevirtualize
和 -fdevirtualize-speculatively
。这些名称暗示了质量的保证水平。
关于c++ - 确定性时的虚函数开销 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322010/
我使用 Deflater 编写了一个备份程序SHA-1 用于存储文件和哈希值。我看到Java的Deflater使用zlib 。如果我显式设置 Deflater 的级别,无论平台和 JRE 版本如何,我
考虑以下算法: r = 2 while r >= 1: x = -1 + 2 * random.random() y = -1 + 2 * random.random() r
我正在编写一个持久保存到磁盘的映射类。我目前只允许 str键,但如果我可以使用更多类型会很好:希望最多可以是任何可散列的(即与内置 dict 相同的要求),但更合理的是我会接受字符串、unicode、
我有一个不使用随机化的脚本,当我运行它时会给出不同的答案。我希望每次运行脚本时答案都是一样的。该问题似乎只发生在某些(病态)输入数据上。 该代码段来自一种计算线性系统特定类型 Controller 的
这对我来说不是问题,没有它我也可以生活,但我只是好奇这是否可能以及如何实现。 今天我了解到,scrapy.Request 不会按照启动的顺序完成。 伪代码示例: class SomeSpider(sc
我正在运行这个 SVD来自 scipy 的求解器,代码如下: import numpy as np from scipy.sparse.linalg import svds features = np
我正在尝试使用确定性 Miller-Rabin 算法实现素数检查功能,但结果并不总是正确的:在检查前 1,000,000 个数字时,它只找到 78,495 而不是 78,498。 这是使用 [2, 7
我正在审查各种 Android 声音 API,我想知道我应该使用哪一个。我的目标是获得低延迟的音频,或者至少是关于播放延迟的确定性行为。 我们遇到了很多问题,Android 声音 API 似乎很垃圾,
过去,我处理过对时间要求严格的软件开发。这些应用程序的开发基本上是这样进行的:“让我们编写代码,测试延迟和抖动,并优化它们,直到它们在可接受的范围内。”我觉得这非常令人沮丧。这不是我所说的 适当的工程
给定: SQL Server 表名为 TEST_TABLE TEST_TABLE 中名为 TEST_FIELD 的列 VARCHAR(50) NOT NULL 第 1 行:10YR3/6 第 2 行:
我在 64 位 Windows PC 上使用 Microsoft Visual Studio Community 2015,版本 14.xxx。 程序读取一个文本文件,其中每一行都是桥牌(四名玩家,每
我需要在 PHP 中创建一个可证明公平(确定性和种子)加密安全 (CS) 随机数生成器。我们正在运行 PHP 5,而 PHP 7 现在并不是一个真正的选择。但是,我找到了 PHP 7 的新 CS 函数
我是一名优秀的程序员,十分优秀!