Redian新闻
>
Do the two statements cost the same amount of time?
avatar
Do the two statements cost the same amount of time?# Programming - 葵花宝典
t*s
1
1) for i =1 to floor(sqrt(n))
2) for i=1 to n
I guess that they take different amount of time.
The reason is that in the first one each check to see whether i exceeds
the limit involves a floor(sqrt(n)) operation, while the second one
does not.
Or maybe the limit 'floor(sqrt(n))' is calculated only once and therefore I
am wrong ?
Can somebody confirm please?
avatar
l*d
2
Why not try to run a short piece of code and compare
the run time
1. x= 1e16+0.1;
for (i=1; i
2. x= 1e16+0.1;
for (i=1; i
I

【在 t**********s 的大作中提到】
: 1) for i =1 to floor(sqrt(n))
: 2) for i=1 to n
: I guess that they take different amount of time.
: The reason is that in the first one each check to see whether i exceeds
: the limit involves a floor(sqrt(n)) operation, while the second one
: does not.
: Or maybe the limit 'floor(sqrt(n))' is calculated only once and therefore I
: am wrong ?
: Can somebody confirm please?

avatar
v*u
3

I
我相信大部分编译器会做这个优化

【在 t**********s 的大作中提到】
: 1) for i =1 to floor(sqrt(n))
: 2) for i=1 to n
: I guess that they take different amount of time.
: The reason is that in the first one each check to see whether i exceeds
: the limit involves a floor(sqrt(n)) operation, while the second one
: does not.
: Or maybe the limit 'floor(sqrt(n))' is calculated only once and therefore I
: am wrong ?
: Can somebody confirm please?

avatar
a*l
4
嘿嘿,我相信编译器绝对是不会做这个优化的,因为这个优化会非常难的,if ever
possible.理由自己想.

【在 v*****u 的大作中提到】
:
: I
: 我相信大部分编译器会做这个优化

avatar
g*y
5
If 'n' is not referenced/modified inside the loop, this should be a very
safe optimization for the compiler.
avatar
a*l
6
well, that's a very big "if"...

【在 g*****y 的大作中提到】
: If 'n' is not referenced/modified inside the loop, this should be a very
: safe optimization for the compiler.

avatar
e*e
7
I think in general the compiler is unlikely to optimize this and evaluate
the expression only once at the beginning of the loop. There are too
many ways in C that the value of n can be modified directly and
indirectly (pointer, global variable, etc.)
In Fortran, the final value of the do-loop variable must be evaluated
only once at the beginning of the loop, and this will give the (maximum)
number of times the loop will be executed.

【在 g*****y 的大作中提到】
: If 'n' is not referenced/modified inside the loop, this should be a very
: safe optimization for the compiler.

avatar
j*r
8
That depends on the compiling option and your compiler.
If you use GCC, you can use option -O to specify the optimization level you
want. If you use full optimization(-O3), I think the compiler will try to
optimize it. If variable n is not modified inside the loop, it will be
optimized of course. If variable n is modified inside the loop, I don't
think the compiler will optimize it. If it is difficult for the compiler to
judge between these two situations, I guess the complier takes a
conservati

【在 t**********s 的大作中提到】
: 1) for i =1 to floor(sqrt(n))
: 2) for i=1 to n
: I guess that they take different amount of time.
: The reason is that in the first one each check to see whether i exceeds
: the limit involves a floor(sqrt(n)) operation, while the second one
: does not.
: Or maybe the limit 'floor(sqrt(n))' is calculated only once and therefore I
: am wrong ?
: Can somebody confirm please?

avatar
l*j
9
搞个 objdump 看一下汇编码。
avatar
c*x
10
they should be differet, but hard to messure, i guess,
since all the variables loaded in the CPU's registors.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。