From: "Paul Ranson"
This is a multi-part message in MIME format.
------=_NextPart_000_000F_01C4BB76.032814F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I don't understand the point of this program.
Why do you want to display the numbers 1-1000000000? You won't be able = to
see them. And you'll be measuring the performance of the display = system
right down to the graphics card driver rather than any code = you're
writing.
FWIW this,
unsigned u ;
while ( u < 1000000000 )
++u ;
runs in 1500 odd CPU cycles. Because it doesn't do anything so the =
compiler optimises it away. The 1500 cycles is pretty much the overhead =
of measurement.
Here's an idea for measuring time,
#include
#include
#include
class Timer
{
private :
std::string Context ;
LARGE_INTEGER Start ;
LARGE_INTEGER End ;
public :
Timer ( const char * s ) : Context ( s )
{
::QueryPerformanceCounter ( &Start ) ;
}
~Timer ()
{
::QueryPerformanceCounter ( &End ) ;
LONGLONG d =3D End.QuadPart - Start.QuadPart ;
std::cout << Context << " duration : "
<< d << " cycles\n" ;
}
} ;
A possible test might be something like,
#include
float TestFloat ( std::complex c ) {
unsigned n ;
Timer t ( "Test Float" ) ;
for ( n =3D 0; n < 10000; ++n )
c =3D c * c + c ;
return abs ( c ) ;
}
double TestDouble ( std::complex c ) {
unsigned n ;
Timer t ( "TestDouble" ) ;
for ( n =3D 0; n < 10000; ++n )
c =3D c * c + c ;
return abs ( c ) ;
}
int main()
{
std::cout << "TestFloat returns " << TestFloat (
std::complex( =
0.1F, 0.75F )) << "\n" ;
std::cout << "TestDouble returns " << TestDouble ( =
std::complex( 0.1, 0.75 )) << "\n" ;
return 0;
}
For which I get the output,
Test Float duration : 987260 ticks
TestFloat returns 0.000100005
TestDouble duration : 305220 ticks
TestDouble returns 0.000100005
Which sort of implies that double is faster on this occasion. It's up to =
you to work out why...
Paul
"Geo" wrote in message
news:417d7dec$1{at}w3.nls.net...
> While optimizing some random piece of code may not matter, I've =
already had
> opportunity in C++ to see where it does matter and so I try to =
understand
> how to best optimize some code in order to learn. To make my point =
here's a
> simple but fun challenge.
>=20
> Write a cmd window program (as in non-GUI) that displays the time, =
counts
> from 1 to 1 billion displaying the counter on the screen as it counts, =
then
> display the time again and the elapsed time and end with the "press a =
key to
> continue" prompt so it's easy to view. Yes I know we are working on
> multitasking systems so other processes will effect it but I've played =
with
> this a little and found some huge differences in the time it takes =
using
> different methods. I haven't learned the time function yet but the
> differences I've found didn't require a clock to know which was =
shorter.
>=20
> Whoever does this with the shortest elapsed time wins.
>=20
> When I was doing 6502 assembler we organized a challenge like this (we =
only
> counted to a million), the winner had what I can only describe as the =
most
> elegant program I've ever seen and he beat the next closest by a =
factor of
> 2. I don't know enough about C++ to do this justice but I can =
guarantee you
> that if you think you know the language and architecture well, this =
will
> really make you think.
>=20
> Geo.
>=20
> "Rich" wrote in message news:417cfebb$1{at}w3.nls.net...
> It has nothing to do with C++. In any program there are things that
> matter and things that don't. Don't waste your time optimizing =
something
> that doesn't matter.
>=20
> Rich
>=20
>
------=_NextPart_000_000F_01C4BB76.032814F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
I don't understand the point of this =
program.
Why do you want to display the numbers
1-1000000000? =
You won't=20
be able to see them. And you'll be measuring the performance of the = display=20
system right down to the graphics card driver rather than any code = you're=20
writing.
FWIW this,
unsigned u =
;
while ( u < =
1000000000=20
)
++u=20
;
runs in 1500 odd CPU cycles. Because it doesn't do =
anything so=20
the compiler optimises it away. The 1500 cycles is pretty much the = overhead of=20
measurement.
Here's an idea for measuring
time,
#include=20
<windows.h>
#include=20
<string>
#include=20
<iostream>
class =
Timer
{
private =
:
=20
std::string Context ;
=20
LARGE_INTEGER Start ;
=20
LARGE_INTEGER End ;
public =
:
Timer (=20 const char * s ) :
Context ( s )
=20
{
=
::QueryPerformanceCounter (=20
&Start ) ;
=20
}
~Timer=20 ()
=20
{
=
::QueryPerformanceCounter (=20
&End ) ;
LONGLONG d =3D = End.QuadPart -=20
Start.QuadPart ;
std::cout << = Context=20
<< " duration : " << d
<< " cycles\n" ;
=20
}
} =
;
A possible test might be something =
like,
#include=20
<complex>
float TestFloat =
(=20
std::complex<float> c
){ unsigned n ; Timer = t
(=20
"Test Float" ) ; for ( n =3D 0; n < 10000; ++n =
) c =3D c=20
* c + c ;
return =
abs ( c )=20
;}
double =
TestDouble (=20
std::complex<double> c
){ unsigned n ; Timer = t
(=20
"TestDouble" ) ; for ( n =3D 0; n < 10000; ++n =
) c =3D c=20
* c + c ;
return =
abs ( c )=20
;}
int=20
main(){ std::cout <<
"TestFloat returns " << = TestFloat=20
( std::complex<float>( 0.1F, 0.75F )) << "\n" =
; std::cout=20
<< "TestDouble returns " << TestDouble ( =
std::complex<double>(=20
0.1, 0.75 )) << "\n" ;
return=20
0;}
For which I get the output,
Test Float =
duration : 987260=20
ticksTestFloat returns 0.000100005TestDouble duration :
305220=20 ticksTestDouble returns
0.000100005
Which sort of implies that double is faster on this =
occasion.=20
It's up to you to work out why...
Paul
"Geo" <mailto:georger{at}nls.net">
size=3D2>georger{at}nls.net> wrote in message =
news:417d7dec$1{at}w3.nls.net...> While optimizing some random piece of code may not
matter, = I've=20
already had> opportunity in C++ to see where it does
matter and = so I try=20
to understand> how to best optimize some code in order to
learn. = To make=20
my point here's a> simple but fun
challenge.> > = Write a cmd=20
window program (as in non-GUI) that displays the time,
counts> = from 1 to=20
1 billion displaying the counter on the screen as it counts, =
then>=20
display the time again and the elapsed time and end with the "press a = key=20
to> continue" prompt so it's easy to view. Yes I know
we are = working=20
on> multitasking systems so other processes will effect it
but = I've=20
played with> this a little and found some huge differences
in the = time it=20
takes using> different methods. I haven't learned the time
= function yet=20
but the> differences I've found didn't require a clock to
know = which was=20
shorter.> > Whoever does this with the
shortest elapsed = time=20
wins.> > When I was doing 6502 assembler
we organized a = challenge=20
like this (we only> counted to a million), the winner had
what I = can only=20
describe as the most> elegant program I've ever seen and
he beat = the next=20
closest by a factor of> 2. I don't know enough about C++
to do = this=20
justice but I can guarantee you> that if you think you
know the = language=20
and architecture well, this will> really make you
think.> =
>=20
Geo.> > "Rich"
<{at}> wrote in message news:417cfebb$1{at}w3.nls.net...> =20
It has nothing to do with C++. In any program there are things=20
that> matter and things that don't. Don't waste
your time=20 optimizing something> that doesn't
matter.> > = Rich>=20
>
------=_NextPart_000_000F_01C4BB76.032814F0--
--- BBBS/NT v4.01 Flag-5
* Origin: Barktopia BBS Site http://HarborWebs.com:8081 (1:379/45)
SEEN-BY: 633/267 270 5030/786
@PATH: 379/45 1 396/45 106/2000 633/267
|