由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
CS版 - ++ a faster or a++ faster in C?
相关主题
CPU 支持操作系统的问题 (转载)Two interview questions? (转载)
问个问题[公告] CompSci 板的投票结果
问一个C++函数Parameter的问题建议用open64Re: 改GCC 弄出一个特殊的Compiler,可能吗?
Help, install C++ compilerGCC compiler 的问题
想去CS读个MASTERSPAM 2.0
请教一个基础C++问题 (转载)[转载] 问个fortran的基础问题
Ph.D Openings in CS/CE for Spring/Fall 2013An interview question
Java怎么不能用新的版本编译?编译书一问
相关话题的讨论汇总
话题: faster话题: c++话题: increment话题: sometype话题: compiler
进入CS版参与讨论
1 (共1页)
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++?
相关主题
请教一个基础C++问题 (转载)Two interview questions? (转载)
Ph.D Openings in CS/CE for Spring/Fall 2013[公告] CompSci 板的投票结果
Java怎么不能用新的版本编译?建议用open64Re: 改GCC 弄出一个特殊的Compiler,可能吗?
进入CS版参与讨论
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++说的话没错,但是有强烈的误导性
1 (共1页)
进入CS版参与讨论
相关主题
编译书一问想去CS读个MASTER
推荐一个open source的c compiler请教一个基础C++问题 (转载)
How is this conference?Ph.D Openings in CS/CE for Spring/Fall 2013
CASES conference?Java怎么不能用新的版本编译?
CPU 支持操作系统的问题 (转载)Two interview questions? (转载)
问个问题[公告] CompSci 板的投票结果
问一个C++函数Parameter的问题建议用open64Re: 改GCC 弄出一个特殊的Compiler,可能吗?
Help, install C++ compilerGCC compiler 的问题
相关话题的讨论汇总
话题: faster话题: c++话题: increment话题: sometype话题: compiler