Thread: Re: [Dev-C++] loop optimization
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-08-05 23:44:21
|
Ok here is a test I tried on a OLD comp I have lying
around.
System specifications:
25Mhz 486 (no math co-processor)
4Mb Ram
Compiled using the Pacific C compiler (djgpp runs WAY too
slow) using full
optimization.
Program:
#include <stdio.h>
#include <time.h>
long getint(void);
int main()
{
time_t begin,end;
long i, get_int;
begin = time(NULL);
for(i = 0; i < getint(); ++i);
end = time(NULL);
printf("Function call time taken = %i\n", end - begin);
get_int = getint();
begin = time(NULL);
for(i = 0; i < get_int; ++i);
end = time(NULL);
printf("Varible buffer time taken = %i\n", end - begin);
return 0;
}
long getint(void)
{
return 50000000;
}
Output:
Function call time taken = 40
Varible buffer time taken = 16
Hope this helps.
----- Original Message -----
From: "OROSZI Balázs" <or...@vp...>
To: <dev...@li...>
Sent: Tuesday, August 05, 2003 3:17 PM
Subject: Re: [Dev-C++] loop optimization
> Graham Douglas wrote:
> > Run the profiler to find out where the inefficiencies
are. In my
> > experience, any attempt to prejudge optimisation will
be wrong. The
> > inefficiencies are _never_ where you expect.
> >
> > The two fundamental rules for optimising by hand are:
> >
> > rule 1: don't do it.
> > rule 2: don't do it - yet.
>
> Thanks. Advice taken :)
>
> --
> Greetings,
> Balázs
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET
sites including
> Data Reports, E-commerce, Portals, and Forums are
available now.
> Download today and enter to win an XBOX or Visual Studio
.NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE:
http://www23.brinkster.com/noicys/devcpp/ub.htm
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
|
|
From: David M. <ci...@ya...> - 2003-08-06 10:06:54
|
I like dos. Anyway I don't use it for everyday tasks. Only
for doing some
long calculation that I don't want to give my main system.
Someone had asked wether it would be better to constantly
re-call a function
in a for loop or t copy the value to a varible and use that
instead.
The idea was to show the code running on an extremly slow
system (with
absolutely no bells and whistles) so a relatively decent
comparison could be
made. Besides, if I did not have this system I would have
to make getint()
return a much bigger number (most comps today go through
50,000,000 too fast
for time() to give a different time reading)
Who messed with my output? It is supposed to be:
Function call time taken = 40
Varible buffer time taken = 16
----- Original Message -----
From: "Scott Simontis SIMONTIS" <age...@co...>
To: <ci...@ya...>
Sent: Tuesday, August 05, 2003 7:57 PM
Subject: Re: [Dev-C++] loop optimization
That is way too slow to use. I use a laptop with a Pentium
MMX 180 MHz
CPU, 24MB RAM, 1.5GB HD, and dev-cpp for my old computer.
Consider
never using that again.
~~Scott Simontis~~
Programmer
----- Original Message -----
From: David McKen <ci...@ya...>
Date: Tuesday, August 5, 2003 7:44 pm
Subject: Re: [Dev-C++] loop optimization
> Ok here is a test I tried on a OLD comp I have lying
> around.
>
> System specifications:
> 25Mhz 486 (no math co-processor)
> 4Mb Ram
> Compiled using the Pacific C compiler (djgpp runs WAY too
> slow) using full
> optimization.
>
> Program:
> #include <stdio.h>
> #include <time.h>
>
> long getint(void);
>
> int main()
> {
> time_t begin,end;
> long i, get_int;
>
> begin =time(NULL);
> for(i =; i < getint(); ++i);
> end =time(NULL);
> printf("Function call time taken =i\n", end - begin);
>
> get_int =getint();
> begin = time(NULL);
> for(i =; i < get_int; ++i);
> end = time(NULL);
> printf("Varible buffer time taken =i\n", end - begin);
>
> return 0;
> }
>
> long getint(void)
> {
> return 50000000;
> }
>
> Output:
> Function call time taken =0
> Varible buffer time taken =6
>
> Hope this helps.
>
> ----- Original Message -----
> From: "OROSZI Balázs" <or...@vp...>
> To: <dev...@li...>
> Sent: Tuesday, August 05, 2003 3:17 PM
> Subject: Re: [Dev-C++] loop optimization
>
>
> > Graham Douglas wrote:
> > > Run the profiler to find out where the inefficiencies
> are. In my
> > > experience, any attempt to prejudge optimisation will
> be wrong. The
> > > inefficiencies are _never_ where you expect.
> > >
> > > The two fundamental rules for optimising by hand are:
> > >
> > > rule 1: don't do it.
> > > rule 2: don't do it - yet.
> >
> > Thanks. Advice taken :)
> >
> > --
> > Greetings,
> > Balázs
> >
> >
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
|
|
From: Luís B. <lf...@ya...> - 2003-08-06 11:32:31
|
--- David McKen <ci...@ya...> wrote:
> I like dos. Anyway I don't use it for everyday tasks. Only
> for doing some
> long calculation that I don't want to give my main system.
>
> Someone had asked wether it would be better to constantly
> re-call a function
> in a for loop or t copy the value to a varible and use that
> instead.
>
> The idea was to show the code running on an extremly slow
> system (with
> absolutely no bells and whistles) so a relatively decent
> comparison could be
> made. Besides, if I did not have this system I would have
> to make getint()
> return a much bigger number (most comps today go through
> 50,000,000 too fast
> for time() to give a different time reading)
>
> Who messed with my output? It is supposed to be:
> Function call time taken = 40
> Varible buffer time taken = 16
>
>
>
> ----- Original Message -----
> From: "Scott Simontis SIMONTIS" <age...@co...>
> To: <ci...@ya...>
> Sent: Tuesday, August 05, 2003 7:57 PM
> Subject: Re: [Dev-C++] loop optimization
>
>
> That is way too slow to use. I use a laptop with a Pentium
> MMX 180 MHz
> CPU, 24MB RAM, 1.5GB HD, and dev-cpp for my old computer.
> Consider
> never using that again.
>
> ~~Scott Simontis~~
> Programmer
>
> ----- Original Message -----
> From: David McKen <ci...@ya...>
> Date: Tuesday, August 5, 2003 7:44 pm
> Subject: Re: [Dev-C++] loop optimization
>
> > Ok here is a test I tried on a OLD comp I have lying
> > around.
> >
> > System specifications:
> > 25Mhz 486 (no math co-processor)
> > 4Mb Ram
> > Compiled using the Pacific C compiler (djgpp runs WAY too
> > slow) using full
> > optimization.
> >
> > Program:
> > #include <stdio.h>
> > #include <time.h>
> >
> > long getint(void);
> >
> > int main()
> > {
> > time_t begin,end;
> > long i, get_int;
> >
> > begin =time(NULL);
> > for(i =; i < getint(); ++i);
> > end =time(NULL);
> > printf("Function call time taken =i\n", end - begin);
> >
> > get_int =getint();
> > begin = time(NULL);
> > for(i =; i < get_int; ++i);
> > end = time(NULL);
> > printf("Varible buffer time taken =i\n", end - begin);
> >
> > return 0;
> > }
> >
> > long getint(void)
> > {
> > return 50000000;
> > }
> >
> > Output:
> > Function call time taken =0
> > Varible buffer time taken =6
> >
> > Hope this helps.
> >
> > ----- Original Message -----
> > From: "OROSZI Balázs" <or...@vp...>
> > To: <dev...@li...>
> > Sent: Tuesday, August 05, 2003 3:17 PM
> > Subject: Re: [Dev-C++] loop optimization
> >
> >
> > > Graham Douglas wrote:
> > > > Run the profiler to find out where the inefficiencies
> > are. In my
> > > > experience, any attempt to prejudge optimisation will
> > be wrong. The
> > > > inefficiencies are _never_ where you expect.
> > > >
> > > > The two fundamental rules for optimising by hand are:
> > > >
> > > > rule 1: don't do it.
> > > > rule 2: don't do it - yet.
> > >
> > > Thanks. Advice taken :)
> > >
> > > --
> > > Greetings,
> > > Balázs
> > >
> > >
>
>
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site design software
> http://sitebuilder.yahoo.com
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
#include <stdio.h>
#include <time.h>
long get_int( void )
{
return 50000000;
}
int main(int argc, char *argv[])
{
// Time variables.
time_t start_time, end_time, split_time;
long i, getint;
// Initialize clock
start_time = clock();
for ( i =1; i < get_int(); ++i);
// end time
end_time = clock();
//calculate split time
split_time = end_time - start_time;
printf( "+-+-+ Function Call time +-+-+\n" );
// write the time to console
printf( "Time: %5.3f secs.\n", (double)(split_time)/CLOCKS_PER_SEC );
getint = get_int();
// Initialize clock
start_time = clock();
for ( i =1; i < getint; ++i);
// end time
end_time = clock();
//calculate split time
split_time = end_time - start_time;
// write the time to console
printf( "+-+-+ Variable Buffer time +-+-+\n" );
printf( "Time: %5.3f secs.\n", (double)(split_time)/CLOCKS_PER_SEC );
// ... press any key
printf( "Press ENTER to continue...\n" );
getchar();
return 0;
}
output:
+-+-+ Function Call time +-+-+
Time: 0.540 secs.
+-+-+ Variable Buffer time +-+-+
Time: 0.481 secs.
Press ENTER to continue...
compiler:
Mingw version 3.2
GUI:
Dev-C++
Computer:
Pentium III 863MHz 128MM RAM
OS:
Microsoft Windows XP
Profissional
Version 2002
Service Pack 1
=====
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Luís Filipe Delgado Brás
Eng.º Civil
Porto - Portugal
email: lf...@ya...
http://www.fotopt.net/autor.asp?autor=2637
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
|
|
From: <or...@vp...> - 2003-08-06 11:53:03
|
David McKen wrote:
> Ok here is a test I tried on a OLD comp I have lying
> around.
>
> System specifications:
> 25Mhz 486 (no math co-processor)
> 4Mb Ram
> Compiled using the Pacific C compiler (djgpp runs WAY too
> slow) using full
> optimization.
>
> Program:
> #include <stdio.h>
> #include <time.h>
>
> long getint(void);
>
> int main()
> {
> time_t begin,end;
> long i, get_int;
>
> begin = time(NULL);
> for(i = 0; i < getint(); ++i);
> end = time(NULL);
> printf("Function call time taken = %i\n", end - begin);
>
> get_int = getint();
> begin = time(NULL);
> for(i = 0; i < get_int; ++i);
> end = time(NULL);
> printf("Varible buffer time taken = %i\n", end - begin);
>
> return 0;
> }
>
> long getint(void)
> {
> return 50000000;
> }
>
> Output:
> Function call time taken = 40
> Varible buffer time taken = 16
>
> Hope this helps.
Thank you very much. Actually, I was also doing some benchmarks, and got
the same result as you. However, that getint() function is not that
simple :) It does something more complicated.
Anyway, thanks for it.
--
Greetings,
Balázs
|
|
From: Per W. <pw...@ia...> - 2003-08-06 12:17:37
|
As Ioannis already mentioned, the important factor is if the compiler
knows the implementation of the function.
If the compiler has access to the code and can check that it returns a
constant value and doesn't have any side effects, it may - depending on
optimization settings etc - retrieve the end value before intering the
loop.
Whenever the compiler is in doublt, it has to assume that the function has
side effects or returns a nonconstant value, requiring the function to be
called - or the contents of the function to be inlined - for every
iteration of the loop.
/Per W
On Wed, 6 Aug 2003, OROSZI Bal=E1zs wrote:
> David McKen wrote:
> > Ok here is a test I tried on a OLD comp I have lying
> > around.
> >
> > System specifications:
> > 25Mhz 486 (no math co-processor)
> > 4Mb Ram
> > Compiled using the Pacific C compiler (djgpp runs WAY too
> > slow) using full
> > optimization.
> >
> > Program:
> > #include <stdio.h>
> > #include <time.h>
> >
> > long getint(void);
> >
> > int main()
> > {
> > time_t begin,end;
> > long i, get_int;
> >
> > begin =3D time(NULL);
> > for(i =3D 0; i < getint(); ++i);
> > end =3D time(NULL);
> > printf("Function call time taken =3D %i\n", end - begin);
> >
> > get_int =3D getint();
> > begin =3D time(NULL);
> > for(i =3D 0; i < get_int; ++i);
> > end =3D time(NULL);
> > printf("Varible buffer time taken =3D %i\n", end - begin);
> >
> > return 0;
> > }
> >
> > long getint(void)
> > {
> > return 50000000;
> > }
> >
> > Output:
> > Function call time taken =3D 40
> > Varible buffer time taken =3D 16
> >
> > Hope this helps.
>
> Thank you very much. Actually, I was also doing some benchmarks, and got
> the same result as you. However, that getint() function is not that
> simple :) It does something more complicated.
>
> Anyway, thanks for it.
>
> --
> Greetings,
> Bal=E1zs
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET sites including
> Data Reports, E-commerce, Portals, and Forums are available now.
> Download today and enter to win an XBOX or Visual Studio .NET.
> http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/=
01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|