由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 有个SB interviewer和我说++i比i++好
相关主题
C的argc问题c++ 问题
*d++ = *s++请教关于epic的电面和assessment test
leetcode上一题,求正解这道题怎么做的?
一道C语言题一道题,我觉得挺难
请教一个入门级的C的指针问题贡献面经 amazon, 虽然面挂了,还是攒点人品
几道a家onsite问题讨论贴面经
几个C语言的题目微软面经
考古题,大家来讨论啊求一个Amazon经常问的问题的答案。。。
相关话题的讨论汇总
话题: test话题: r3话题: operator话题: value话题: mov
进入JobHunting版参与讨论
1 (共1页)
s******n
发帖数: 3946
1
他的意思是假设是operator重载
++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
设编译无优化)
大家看有道理吗?
I did a real test on arm compiler turn off optimization:
the O0 code is exactly the same, except that post operator++()(int dummy)
has an extra dummy parameter which is required by c++ to identify the
difference of prefix and postfix.
2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
printf("%d \n", 10+ (abc++).value);
sub r3, fp, #8
mov r0, r3
mov r1, #0 --> the dummy parameter of postfix
bl _ZN4TestppEi --> call the overload function
mov r3, r0
ldr r3, [r3, #0]
add r3, r3, #10
ldr r0, .L12
mov r1, r3
bl printf
printf("%d \n", 20+ (++abc).value);
sub r3, fp, #8
mov r0, r3
bl _ZN4TestppEv --> call the overload function
mov r3, r0
ldr r3, [r3, #0]
add r3, r3, #20
ldr r0, .L12
mov r1, r3
bl printf
c++ source code
#include
class Test {
public:
Test(int v):value(v){};
Test():value(0){};
int value;
int operator()();
Test& operator++();
Test& operator++(int postVersion);
};
Test& Test::operator++(){
value++;
return *this;
}
Test& Test::operator++(int postVersion){
value++;
return *this;
}
int Test::operator()() {
return value;
}
int main(int argc, char** argv)
{
Test abc(100);
printf("%d \n", 10+ (abc++).value);
printf("%d \n", 20+ (++abc).value);
}
h**********y
发帖数: 1293
2
这是基本常识。
h**********l
发帖数: 6342
3
。。。。
sure yes

【在 s******n 的大作中提到】
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)
: has an extra dummy parameter which is required by c++ to identify the
: difference of prefix and postfix.
: 2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
: printf("%d \n", 10+ (abc++).value);

s******n
发帖数: 3946
4
具体展开一下,大致汇编代码是怎么样的,两种情况下?

【在 h**********y 的大作中提到】
: 这是基本常识。
h**********l
发帖数: 6342
5
你不是自己都说了吗?
基本类型也是一样的

【在 s******n 的大作中提到】
: 具体展开一下,大致汇编代码是怎么样的,两种情况下?
H***e
发帖数: 476
6
G?
他们不问这种的吧?

【在 s******n 的大作中提到】
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)
: has an extra dummy parameter which is required by c++ to identify the
: difference of prefix and postfix.
: 2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
: printf("%d \n", 10+ (abc++).value);

s******n
发帖数: 3946
7
不是G

【在 H***e 的大作中提到】
: G?
: 他们不问这种的吧?

m***n
发帖数: 2154
8
hehe ,简单的i++和++i 区别不大,但如果是对象operator重载,区别很大,呵呵
l***i
发帖数: 1309
9
give me an example that i++ is better. In general i++ needs to save i in a
temp variable and then incr i because you need to return that temp value. My
understanding is that ++i is always better than i++.
d**********o
发帖数: 279
10
这个除了优化些, 而且避免中断产生的race problem,因为前者是atom 操作。
相关主题
几道a家onsite问题讨论贴c++ 问题
几个C语言的题目请教关于epic的电面和assessment test
考古题,大家来讨论啊这道题怎么做的?
进入JobHunting版参与讨论
p*i
发帖数: 411
11
If i is an integer or something, i++ is merely a single instruction on intel
architectures.

My

【在 l***i 的大作中提到】
: give me an example that i++ is better. In general i++ needs to save i in a
: temp variable and then incr i because you need to return that temp value. My
: understanding is that ++i is always better than i++.

s******n
发帖数: 3946
12
the O0 code is exactly the same, except that post operator++()(int dummy)
has an extra dummy parameter which is required by c++ to identify the
difference of prefix and postfix.
2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
printf("%d \n", 10+ (abc++).value);
sub r3, fp, #8
mov r0, r3
mov r1, #0 --> the dummy parameter of postfix
bl _ZN4TestppEi
mov r3, r0
ldr r3, [r3, #0]
add r3, r3, #10
ldr r0, .L12
mov r1, r3
bl printf
printf("%d \n", 20+ (++abc).value);
sub r3, fp, #8
mov r0, r3
bl _ZN4TestppEv
mov r3, r0
ldr r3, [r3, #0]
add r3, r3, #20
ldr r0, .L12
mov r1, r3
bl printf
c++ source code
#include
class Test {
public:
Test(int v):value(v){};
Test():value(0){};
int value;
int operator()();
Test& operator++();
Test& operator++(int postVersion);
};
Test& Test::operator++(){
value++;
return *this;
}
Test& Test::operator++(int postVersion){
value++;
return *this;
}
int Test::operator()() {
return value;
}
int main(int argc, char** argv)
{
Test abc(100);
printf("%d \n", 10+ (abc++).value);
printf("%d \n", 20+ (++abc).value);
}
d****z
发帖数: 314
13
....yes

【在 s******n 的大作中提到】
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)
: has an extra dummy parameter which is required by c++ to identify the
: difference of prefix and postfix.
: 2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
: printf("%d \n", 10+ (abc++).value);

s******o
发帖数: 2233
14
c++ FAQ 13.15
http://www.parashift.com/c++-faq-lite/operator-overloading.html
"++i is sometimes faster than, and is never slower than, i++"

still generates the same code.

【在 s******n 的大作中提到】
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)
: has an extra dummy parameter which is required by c++ to identify the
: difference of prefix and postfix.
: 2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
: printf("%d \n", 10+ (abc++).value);

a****l
发帖数: 8211
15
都已经假设编译器无优化了,还谈什么效率?这种问题真是可笑。有优化的条件下,这种
问题是蔑视编译器设计者的智商。

still generates the same code.

【在 s******n 的大作中提到】
: 他的意思是假设是operator重载
: ++i先做++再放在stack上,i++则先复制一份copy到stack上再做++,多了一份复制(假
: 设编译无优化)
: 大家看有道理吗?
: I did a real test on arm compiler turn off optimization:
: the O0 code is exactly the same, except that post operator++()(int dummy)
: has an extra dummy parameter which is required by c++ to identify the
: difference of prefix and postfix.
: 2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
: printf("%d \n", 10+ (abc++).value);

S**I
发帖数: 15689
16
Of course you get the same assembly: you overloaded both ++ operators in the
exact same way. Post-increment operator should be overloaded as following:
Test Test::operator++(int postVersion){
Test temp (*this);
value++;
return temp;
}


still generates the same code.
postfix

【在 s******n 的大作中提到】
: the O0 code is exactly the same, except that post operator++()(int dummy)
: has an extra dummy parameter which is required by c++ to identify the
: difference of prefix and postfix.
: 2nd, even I change the Test& operator++() into Test operator++(), it still generates the same code.
: printf("%d \n", 10+ (abc++).value);
: sub r3, fp, #8
: mov r0, r3
: mov r1, #0 --> the dummy parameter of postfix
: bl _ZN4TestppEi
: mov r3, r0

s******n
发帖数: 3946
17
这个是正解了!

the

【在 S**I 的大作中提到】
: Of course you get the same assembly: you overloaded both ++ operators in the
: exact same way. Post-increment operator should be overloaded as following:
: Test Test::operator++(int postVersion){
: Test temp (*this);
: value++;
: return temp;
: }
:
:
: still generates the same code.

a********m
发帖数: 15480
18
恩。lz代码错了。。。。

the

【在 S**I 的大作中提到】
: Of course you get the same assembly: you overloaded both ++ operators in the
: exact same way. Post-increment operator should be overloaded as following:
: Test Test::operator++(int postVersion){
: Test temp (*this);
: value++;
: return temp;
: }
:
:
: still generates the same code.

h********e
发帖数: 1972
19
LZ代码完全写错了嘛。。。。i++肯定会有一份多余的拷贝的。。
1 (共1页)
进入JobHunting版参与讨论
相关主题
求一个Amazon经常问的问题的答案。。。请教一个入门级的C的指针问题
贴个FLEXTRADE的在线C++测试的题几道a家onsite问题讨论贴
24点程序,有人能现场写出来么?几个C语言的题目
那个24 game given 4 number用= - × /的题考古题,大家来讨论啊
C的argc问题c++ 问题
*d++ = *s++请教关于epic的电面和assessment test
leetcode上一题,求正解这道题怎么做的?
一道C语言题一道题,我觉得挺难
相关话题的讨论汇总
话题: test话题: r3话题: operator话题: value话题: mov