|
From: Ioannis V. <iv...@em...> - 2003-08-04 22:49:44
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of OROSZI Bal?zs
> Sent: Monday, August 04, 2003 5:20 PM
> To: dev...@li...
> Subject: [Dev-C++] loop optimization
>=20
>=20
> Hi!
>=20
> Now, I have a question.
> I need to do heavy optimizations in the project I am working=20
> on, and I=20
> am asking you, whether will I gain any speedup by optimizing=20
> loops. I am=20
> interested in the for loop exactly. Let's take this example:
>=20
> for(i=3D0;i<getint();i++){
> /* ... */
> }
>=20
> Would it worth it to take that getint() into an outer=20
> variable, and only=20
> use that in the conditional, like this:
>=20
> int c =3D getint();
> for(i=3D0;i<c;i++){
> /* ... */
> }
You do not specify language (why?) so I assume C.
In C99 if the function can be inlined (and you have used the keyword =
inline)
it will be inlined so you do not get any gain. If it cannot be inlined =
then
by placing it outside the loop you gain some run-time (the time cost of =
the
subsequent funtion calls, which is much if the calls are many).
>=20
> Will this give me any speedup? I am interested in particular, is for=20
> going to call getint() EVERY time in the first case (on top),=20
> or will it=20
> do it only once? I think the second case is better, am I right?
>=20
> Allright, that was my first question, here is the second:
> What is the case, if the conditional is a KNOWN value.=20
> Something, like a=20
> macro. Here is the example:
>=20
> #define GETINT (10*5)
>=20
> for(i=3D0;i<GETINT;i++){
> /* ... */
> }
>=20
> I suppose, this will be optimised by the compiler, and this=20
> can even be=20
> unrolled, am I right?
Well in theory the compiler can be smart without any limits. The only =
worst
case scenario assumption you can make is that the loop will be executed =
50
times (the GETINT is inlined of course).
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|