l******e 发帖数: 94 | 1 I found this question in CareerCup. someone there says ++a is faster, but i
don't know why?
Anybody can tell me the reason? | t*s 发帖数: 1504 | 2 a++ is faster
because a is closer to your finger, + is further away
when you type a++, you first type a, at the same time, your other hand has
already moved half way to +, so it saved significant time.
on the other hand, when you type ++a, your left hand is simply hanged there,
there is sync overhead.
i
【在 l******e 的大作中提到】 : I found this question in CareerCup. someone there says ++a is faster, but i : don't know why? : Anybody can tell me the reason?
| b***n 发帖数: 3 | 3 ft
there,
【在 t*s 的大作中提到】 : a++ is faster : because a is closer to your finger, + is further away : when you type a++, you first type a, at the same time, your other hand has : already moved half way to +, so it saved significant time. : on the other hand, when you type ++a, your left hand is simply hanged there, : there is sync overhead. : : i
| l******e 发帖数: 94 | 4 tong ft. anyone here can tell me the real reason.
【在 b***n 的大作中提到】 : ft : : there,
| i****c 发帖数: 102 | 5 funny,haha
there,
【在 t*s 的大作中提到】 : a++ is faster : because a is closer to your finger, + is further away : when you type a++, you first type a, at the same time, your other hand has : already moved half way to +, so it saved significant time. : on the other hand, when you type ++a, your left hand is simply hanged there, : there is sync overhead. : : i
| a****1 发帖数: 61 | 6 good reason. i usually type a++ rather than ++a.
there,
【在 t*s 的大作中提到】 : a++ is faster : because a is closer to your finger, + is further away : when you type a++, you first type a, at the same time, your other hand has : already moved half way to +, so it saved significant time. : on the other hand, when you type ++a, your left hand is simply hanged there, : there is sync overhead. : : i
| c*****g 发帖数: 119 | 7 old question
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15
i
【在 l******e 的大作中提到】 : I found this question in CareerCup. someone there says ++a is faster, but i : don't know why? : Anybody can tell me the reason?
| m****0 发帖数: 30 | 8 ++a does not require allocating a temp variable while a++ does. | f**********n 发帖数: 3 | 9 no big deal, the compiler will optimize it
【在 m****0 的大作中提到】 : ++a does not require allocating a temp variable while a++ does.
| m****0 发帖数: 30 | 10 How does compiler optimize it in C++? | | | r***u 发帖数: 320 | 11 a++ is faster?
a++ read firstly, and then write.
++a is first write.
so cpu have to wait several cycles. (data hazard in Computer Architecture)
i
【在 l******e 的大作中提到】 : I found this question in CareerCup. someone there says ++a is faster, but i : don't know why? : Anybody can tell me the reason?
| m****0 发帖数: 30 | 12 Totally wrong!
This is c and c++ -- high level programming language.
In fact, for some programming language, there is no difference between pre-
increment and post-increment. However, in c++, when you learn operator
overloading, you will see the difference.
A typical implementation of post-increment is like this:
SomeType SomeType::operator++(int)
{
SomeType temp(*this); // save current object
operator++(); // increment internal state using pre-increment
return temp; /
【在 r***u 的大作中提到】 : a++ is faster? : a++ read firstly, and then write. : ++a is first write. : so cpu have to wait several cycles. (data hazard in Computer Architecture) : : i
| k****z 发帖数: 550 | 13 This is overloading, a different question.
For the ++ operator for primitive types, compiler should have optimized it.
Anybody know the answer for primitive types?
【在 m****0 的大作中提到】 : Totally wrong! : This is c and c++ -- high level programming language. : In fact, for some programming language, there is no difference between pre- : increment and post-increment. However, in c++, when you learn operator : overloading, you will see the difference. : A typical implementation of post-increment is like this: : SomeType SomeType::operator++(int) : { : SomeType temp(*this); // save current object : operator++(); // increment internal state using pre-increment
| r***u 发帖数: 320 | 14 you know there is efficiency issue in high language? compiler optimization
can't do everything.
【在 m****0 的大作中提到】 : Totally wrong! : This is c and c++ -- high level programming language. : In fact, for some programming language, there is no difference between pre- : increment and post-increment. However, in c++, when you learn operator : overloading, you will see the difference. : A typical implementation of post-increment is like this: : SomeType SomeType::operator++(int) : { : SomeType temp(*this); // save current object : operator++(); // increment internal state using pre-increment
| m****0 发帖数: 30 | 15 You replied wrong person.
【在 r***u 的大作中提到】 : you know there is efficiency issue in high language? compiler optimization : can't do everything.
| f*********y 发帖数: 43 | 16 for C, no difference, as I understand.
for class(STL etc) in C++, ++a is more efficient.
i
【在 l******e 的大作中提到】 : I found this question in CareerCup. someone there says ++a is faster, but i : don't know why? : Anybody can tell me the reason?
| c******e 发帖数: 82 | 17 Correct answer.
【在 f*********y 的大作中提到】 : for C, no difference, as I understand. : for class(STL etc) in C++, ++a is more efficient. : : i
| x****u 发帖数: 44466 | 18 如果用的是任何C++标准iterator加上任何商用编译器,答案必然是一样。如果排除任
何一个前提,答案是不确定,hoho。
一定要看Inside C++ Object Model,Effective C++说的话没错,但是有强烈的误导性
。 |
|