Redian新闻
>
C++疑问:Animal a,Animal* a,哪种访问方式快
avatar
C++疑问:Animal a,Animal* a,哪种访问方式快# JobHunting - 待字闺中
f*7
1
如题
C++中,Animal是定义好的class
Animal a
Animal* a
a.foo()
a->foo()
用对象变量 对象指针 访问类的函数或者成员变量
哪种方式更快呢?
求大牛讲解,谢谢您的时间~~
如果有权威链接给出,那再好不过了!
avatar
d*x
2
。。。

【在 f*****7 的大作中提到】
: 如题
: C++中,Animal是定义好的class
: Animal a
: Animal* a
: a.foo()
: a->foo()
: 用对象变量 对象指针 访问类的函数或者成员变量
: 哪种方式更快呢?
: 求大牛讲解,谢谢您的时间~~
: 如果有权威链接给出,那再好不过了!

avatar
p*p
3
前者造出一对象出来
后者造出一指针
你说哪个比较快?

【在 f*****7 的大作中提到】
: 如题
: C++中,Animal是定义好的class
: Animal a
: Animal* a
: a.foo()
: a->foo()
: 用对象变量 对象指针 访问类的函数或者成员变量
: 哪种方式更快呢?
: 求大牛讲解,谢谢您的时间~~
: 如果有权威链接给出,那再好不过了!

avatar
d*x
4
后者不再造出一对象用指针访问谁?

【在 p*****p 的大作中提到】
: 前者造出一对象出来
: 后者造出一指针
: 你说哪个比较快?

avatar
l*d
5
他愿意是问a->b和a.b孰快吧

【在 p*****p 的大作中提到】
: 前者造出一对象出来
: 后者造出一指针
: 你说哪个比较快?

avatar
f*7
6

是这个意思,我已经update问题了,谢谢您。

【在 l******d 的大作中提到】
: 他愿意是问a->b和a.b孰快吧
avatar
g*e
8
指针慢一点吧?不过没啥差别吧
avatar
d*x
10
it really depends.
if foo is a virtual member function, in most of the cases the binding will
be delay to run time, and dereferencing virtual function table is often
slower.
However it is usually a small overhead... Profiling tools are better than
human to find out performance bottleneck in large systems.

【在 f*****7 的大作中提到】
: 如题
: C++中,Animal是定义好的class
: Animal a
: Animal* a
: a.foo()
: a->foo()
: 用对象变量 对象指针 访问类的函数或者成员变量
: 哪种方式更快呢?
: 求大牛讲解,谢谢您的时间~~
: 如果有权威链接给出,那再好不过了!

avatar
d*x
11
用常规的对象通常也要取对象地址。
因为一般成员函数*事实上*需要传入this作为参数。。跑不了的

【在 f*****7 的大作中提到】
:
: 谢谢您的帮助。
: 看来用指针访问,得多用一个instruction,some extra work
: 既然这样,那为什么要用指针呢?

avatar
f*7
12

你说得对
要考虑virtual function
用profile tools
如果就考虑一般情况呢?假设foo是个普通函数。

【在 d**********x 的大作中提到】
: it really depends.
: if foo is a virtual member function, in most of the cases the binding will
: be delay to run time, and dereferencing virtual function table is often
: slower.
: However it is usually a small overhead... Profiling tools are better than
: human to find out performance bottleneck in large systems.

avatar
d*x
13
我觉得没区别。
具体到底是否多出来instruction,请用gcc -S编译出来看。

【在 f*****7 的大作中提到】
:
: 你说得对
: 要考虑virtual function
: 用profile tools
: 如果就考虑一般情况呢?假设foo是个普通函数。

avatar
l*b
14
对象名字本身就是个指针吧? 所以没区别??
avatar
d*x
15
class A {
public:
void foo() {n = 0;}
private:
int n;
};
int main() {
A a;
A* p = new A;
a.foo();
p->foo();
return 0;
}
用gdb调试:
13 a.foo();
6: x/8i 0x400576
0x400576
: mov %rax,-0x18(%rbp)
=> 0x40057a
: lea -0x10(%rbp),%rax
0x40057e
: mov %rax,%rdi
0x400581
: callq 0x40059a
0x400586
: mov -0x18(%rbp),%rax
0x40058a
: mov %rax,%rdi
0x40058d
: callq 0x40059a
0x400592
: mov $0x0,%eax

【在 f*****7 的大作中提到】
:
: 你说得对
: 要考虑virtual function
: 用profile tools
: 如果就考虑一般情况呢?假设foo是个普通函数。

avatar
J*9
16
That depends on where *a is allocated?
Animal a : definitely stack
Animal* a: can point to either a stack object or a heap object.
Access to Stack is faster than to heap.

【在 f*****7 的大作中提到】
: 如题
: C++中,Animal是定义好的class
: Animal a
: Animal* a
: a.foo()
: a->foo()
: 用对象变量 对象指针 访问类的函数或者成员变量
: 哪种方式更快呢?
: 求大牛讲解,谢谢您的时间~~
: 如果有权威链接给出,那再好不过了!

avatar
b*m
18
实际使用中你不用考虑其速度的差别。
avatar
s*n
19
变化太多了。
1.如果是so,需要查PLT,GOT,第一次回page fault, 产生I/O.所以一个libary和程序看
哪个是第一次被调用。不同的静态link总会快于dynamic link.
2. 如果是static function,则一样块。
3. 如果instance 的function,则a应该略快。
4. 如果是vtpr的则要查表,估计二者差不多了,相对于查表的代价。

【在 f*****7 的大作中提到】
: 如题
: C++中,Animal是定义好的class
: Animal a
: Animal* a
: a.foo()
: a->foo()
: 用对象变量 对象指针 访问类的函数或者成员变量
: 哪种方式更快呢?
: 求大牛讲解,谢谢您的时间~~
: 如果有权威链接给出,那再好不过了!

avatar
w*a
20
C++调用成员函数的时,需要知道对象的this指针,由这个this指针来调用它的成员函
数。在汇编级一般是把this指针存入ecx寄存器中,然后call相应的函数地址(代码段
中)。栈变量用lea存this因为要算偏移值,lea可以一次做好sub和mov的操作。堆上的
变量直接mov即可。如果要说代码大小,lea比mov占字节多。如果说效率,访问栈资源
肯定要快写。我什么时候做个测试看看。但是正如楼上大牛所说,你完全可以不用在意
这种效率。
avatar
s*n
21
this是作为参数传到函数中吧,用来访问object field?还是需要this 来做一次寻址?
C++调用成员函数的时,需要知道对象的this指针,由这个this指针来调用它的成员函
avatar
w*a
22
在vc中,debug版是先把this传到ecx,然后直接call。后来我看了下release版,优化
后是要push的。

【在 s*****n 的大作中提到】
: this是作为参数传到函数中吧,用来访问object field?还是需要this 来做一次寻址?
: C++调用成员函数的时,需要知道对象的this指针,由这个this指针来调用它的成员函

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。