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 | | 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 操作。 | | | 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++肯定会有一份多余的拷贝的。。 |
|