由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Do the two statements cost the same amount of time?
相关主题
tail call strange behavior on cl.exeCompiler
C++ optimization question用C++的写的numerical or optimization solver library
Looks like 王垠 was right about Google cultureGCC 居然允许变量长度的向量
About volatile in C在 windows下的C++开发平台是不是 Dev-C++?
瓶颈在哪儿?why int** cannot convert to const int** ?
copy constructor 问题a question about CAST
g++ default optimization error[合集] another question for C++ constant
opencv为什么不用java写???????[合集] GCC source code help
相关话题的讨论汇总
话题: sqrt话题: loop话题: compiler话题: do话题: floor
进入Programming版参与讨论
1 (共1页)
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
9
搞个 objdump 看一下汇编码。
c********x
发帖数: 84
10
they should be differet, but hard to messure, i guess,
since all the variables loaded in the CPU's registors.
1 (共1页)
进入Programming版参与讨论
相关主题
[合集] GCC source code help瓶颈在哪儿?
一个const_cast问题copy constructor 问题
数组指针的问题g++ default optimization error
c++中的inline 函数是做什么的?opencv为什么不用java写???????
tail call strange behavior on cl.exeCompiler
C++ optimization question用C++的写的numerical or optimization solver library
Looks like 王垠 was right about Google cultureGCC 居然允许变量长度的向量
About volatile in C在 windows下的C++开发平台是不是 Dev-C++?
相关话题的讨论汇总
话题: sqrt话题: loop话题: compiler话题: do话题: floor