t**********s 发帖数: 930 | 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? |
l*****d 发帖数: 754 | 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?
|
v*****u 发帖数: 1796 | 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?
|
a****l 发帖数: 8211 | 4 嘿嘿,我相信编译器绝对是不会做这个优化的,因为这个优化会非常难的,if ever
possible.理由自己想.
【在 v*****u 的大作中提到】 : : I : 我相信大部分编译器会做这个优化
|
g*****y 发帖数: 1488 | 5 If 'n' is not referenced/modified inside the loop, this should be a very
safe optimization for the compiler. |
a****l 发帖数: 8211 | 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.
|
e**e 发帖数: 3 | 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.
|
j****r 发帖数: 28 | 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?
|
l***j 发帖数: 300 | |
c********x 发帖数: 84 | 10 they should be differet, but hard to messure, i guess,
since all the variables loaded in the CPU's registors. |