By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

which program will run better:)

P: n/a
I write a program which returns the maximum of three input integers.
both can run weil in the DEV-C++ , but I just wonder which one is
better,
and I also want to make it clear that if I use the function, I can
exert the
x,y,z in the funciton , then when apply the x,y,z in the main, there
is no error, but in my text book, some codes will use x,y,z in the
function then x1,x2,x3 in main, so what is difference between these
usage? please give me a help:)
thank you very much:)


/*this grogram find the maxmun of the three given numbers*/

int max(int,int,int);

int max(int a,int b)
{
if(a<b) return b;
else return a;
}
int max(int x, int y, int z)
{ if(z<x) return max(x,y);
else return max(y,z);
}
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"input three integers here"<<endl;
cin>>x>>y>>z;
cout<<max(x,y,z)<<endl;
return 0;
}

/*this grogram find the maxmun of the three given numbers*/

int max(int,int,int);

/*return the maxmun of two integers*/
int max(int a,int b)
{
if(a<b) return b;
else return a;
}

int max(int x, int y, int z)
{ if(z<x) return max(x,y);
else return max(y,z);
}

#include<iostream>
using namespace std;
int main()
{
int x1,x2,x3;
cout<<"input three integers here"<<endl;
cin>>x1>>x2>>x3;
cout<<max(x1,x2,x3)<<endl;
return 0;
}


/* with the using of reference,this grogram find the maxmun of the
three given numbers*/

int max(int& ,int&,int&);

/*return the maxmun of two integers*/
int max(int a,int b)
{
if(a<b) return b;
else return a;
}


int max(int& x, int& y, int& z)
{ if(z<x) return max(x,y);
else return max(y,z);
}

#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"input three integers here"<<endl;
cin>>x>>y>>z;
cout<<max(x,y,z)<<endl;
return 0;
}
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"david" <da********@sina.com> wrote...
I write a program which returns the maximum of three input integers.
both can run weil in the DEV-C++ , but I just wonder which one is
better,
You will have to define "better".
and I also want to make it clear that if I use the function, I can
exert the
x,y,z in the funciton , then when apply the x,y,z in the main, there
is no error, but in my text book, some codes will use x,y,z in the
function then x1,x2,x3 in main, so what is difference between these
usage?
No difference.
please give me a help:)
thank you very much:)


/*this grogram find the maxmun of the three given numbers*/

int max(int,int,int);

int max(int a,int b)
{
if(a<b) return b;
else return a;
}
int max(int x, int y, int z)
{ if(z<x) return max(x,y);
else return max(y,z);
}
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"input three integers here"<<endl;
cin>>x>>y>>z;
cout<<max(x,y,z)<<endl;
return 0;
}

/*this grogram find the maxmun of the three given numbers*/

int max(int,int,int);

/*return the maxmun of two integers*/
int max(int a,int b)
{
if(a<b) return b;
else return a;
}

int max(int x, int y, int z)
{ if(z<x) return max(x,y);
else return max(y,z);
}

#include<iostream>
using namespace std;
int main()
{
int x1,x2,x3;
cout<<"input three integers here"<<endl;
cin>>x1>>x2>>x3;
cout<<max(x1,x2,x3)<<endl;
return 0;
}


/* with the using of reference,this grogram find the maxmun of the
three given numbers*/

int max(int& ,int&,int&);

/*return the maxmun of two integers*/
int max(int a,int b)
{
if(a<b) return b;
else return a;
}


int max(int& x, int& y, int& z)
{ if(z<x) return max(x,y);
else return max(y,z);
}

#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"input three integers here"<<endl;
cin>>x>>y>>z;
cout<<max(x,y,z)<<endl;
return 0;
}

Jul 22 '05 #2

P: n/a
> /*this grogram find the maxmun of the three given numbers*/

int max(int,int,int);

/*return the maxmun of two integers*/
int max(int a,int b)
{
if(a<b) return b;
else return a;
}

int max(int x, int y, int z)
{ if(z<x) return max(x,y);
else return max(y,z);
}

#include<iostream>
using namespace std;
int main()
{
int x1,x2,x3;
cout<<"input three integers here"<<endl;
cin>>x1>>x2>>x3;
cout<<max(x1,x2,x3)<<endl;
return 0;
}
you double-posted that but anyway.
/* with the using of reference,this grogram find the maxmun of the
three given numbers*/

int max(int& ,int&,int&);

/*return the maxmun of two integers*/
int max(int a,int b)
{
if(a<b) return b;
else return a;
}

int max(int& x, int& y, int& z)
{ if(z<x) return max(x,y);
else return max(y,z);
}
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"input three integers here"<<endl;
cin>>x>>y>>z;
cout<<max(x,y,z)<<endl;
return 0;
}


if you really care about the time, even in ns (nanoseconds), the 2nd
code is quicker than the 1st one because the 2nd one uses a reference
to the variables and does not spend time allocating new space. The
difference in time though is really small.

i would consider the following quicker:

#include <iostream>
using namespace std;

int man(int &x, int &y, int &z)
{
if (x>y && x>z) return x;
if (y>x && y>z) return y;
return z;
}

int main()
{
int a, b, c;

cout << "type 3 numbers seperated by a space: ";
cin >> a >> b >> c;
cout << "maximun of those numbers = " << max(a,b,c) << endl;

return 0;
}
----
but as i said, it's all a matter of nanoseconds; you don't have to
worry.
Jul 22 '05 #3

P: n/a
<snip>
if you really care about the time, even in ns (nanoseconds), the 2nd
code is quicker than the 1st one because the 2nd one uses a reference
to the variables and does not spend time allocating new space. The
difference in time though is really small.


That is not always true, in fact the first one *may* very well be faster
(just a by only an extremely smal fraction though). Passing by reference is
not necessarily faster for objects that are trivial to copy (like int). When
the compiler inlines the functions there may be no difference at all.

Even if there is a difference I expect the difference to be totally
insignificant for the total performance of the application. The performance
bottleneck is usually somewhere else. But in case the "which is faster"
question is relevant, my advise is try the alternatives and measure which
one is fasted and optionally study assembly output of the compiler to get a
feeling whaat kind of code the compiler generates. Personally I would go for
the code that is the most clear.

BTW. if you pass input parameters by reference, use const references. It
states your intentions more clearly, and the compiler will tell you when you
accidentally try to modify an input parameter.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 22 '05 #4

P: n/a
>
i would consider the following quicker:

#include <iostream>
using namespace std;

int man(int &x, int &y, int &z)
{
if (x>y && x>z) return x;
if (y>x && y>z) return y;
return z;
}

int main()
{
int a, b, c;

cout << "type 3 numbers seperated by a space: ";
cin >> a >> b >> c;
cout << "maximun of those numbers = " << max(a,b,c) << endl;

return 0;
}
----
but as i said, it's all a matter of nanoseconds; you don't have to
worry.


If you really want to get minimalistic, you could go with:

#include <iostream>
int compare(int x, int y, int z){return x>y?x>z?x:z:y>z?y:z;}
int main(){
int x(1), y(-1), z(13);
std::cout << compare(x,y,z);
return 0;
}

of course, the line:
return x>y?x>z?x:z:y>z?y:z;
is really:
return ((x>y)?((x>z)?x:z):((y>z)?y:z));
Jul 22 '05 #5

P: n/a

"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...

i would consider the following quicker:

#include <iostream>
using namespace std;

int man(int &x, int &y, int &z)
{
if (x>y && x>z) return x;
if (y>x && y>z) return y;
return z;
}

int main()
{
int a, b, c;

cout << "type 3 numbers seperated by a space: ";
cin >> a >> b >> c;
cout << "maximun of those numbers = " << max(a,b,c) << endl;

return 0;
}
----
but as i said, it's all a matter of nanoseconds; you don't have to
worry.


If you really want to get minimalistic, you could go with:

#include <iostream>
int compare(int x, int y, int z){return x>y?x>z?x:z:y>z?y:z;}
int main(){
int x(1), y(-1), z(13);
std::cout << compare(x,y,z);
return 0;
}

of course, the line:
return x>y?x>z?x:z:y>z?y:z;
is really:
return ((x>y)?((x>z)?x:z):((y>z)?y:z));


If speed is the most important and you have unlimited memory you could
define an array as follows:

int max_of_3_integers[0xFFFFFFFF][0xFFFFFFFF][0xFFFFFFFF];

Then you initialize the array so that each element:
max_of_3_integers[x][y][z] holds the highest value of the three integers.
Then you could have the macro:

#define max(a,b,c) max_of_3_integers[a][b][c]

to return the maximum integer value out of the three.

Unfortunately the memory required for the array would be 8^96 bytes and it
would probably take a few years to initialise the array. But once the
initialisation had complete you would probably save a few CPU cycles each
time it was called.... :-)
Sean


Jul 22 '05 #6

P: n/a
"Sean Kenwrick" <sk*******@hotmail.com> wrote...

"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...

i would consider the following quicker:

#include <iostream>
using namespace std;

int man(int &x, int &y, int &z)
{
if (x>y && x>z) return x;
if (y>x && y>z) return y;
return z;
}

int main()
{
int a, b, c;

cout << "type 3 numbers seperated by a space: ";
cin >> a >> b >> c;
cout << "maximun of those numbers = " << max(a,b,c) << endl;

return 0;
}
----
but as i said, it's all a matter of nanoseconds; you don't have to
worry.


If you really want to get minimalistic, you could go with:

#include <iostream>
int compare(int x, int y, int z){return x>y?x>z?x:z:y>z?y:z;}
int main(){
int x(1), y(-1), z(13);
std::cout << compare(x,y,z);
return 0;
}

of course, the line:
return x>y?x>z?x:z:y>z?y:z;
is really:
return ((x>y)?((x>z)?x:z):((y>z)?y:z));


If speed is the most important and you have unlimited memory you could
define an array as follows:

int max_of_3_integers[0xFFFFFFFF][0xFFFFFFFF][0xFFFFFFFF];

Then you initialize the array so that each element:
max_of_3_integers[x][y][z] holds the highest value of the three integers.
Then you could have the macro:

#define max(a,b,c) max_of_3_integers[a][b][c]

to return the maximum integer value out of the three.

Unfortunately the memory required for the array would be 8^96 bytes and it
would probably take a few years to initialise the array. But once the
initialisation had complete you would probably save a few CPU cycles each
time it was called.... :-)


Unfortunately, this solution doesn't work. You need to do

int max_of_3_integers[1 << (std::numeric_limits<int>::digits() + 1)]
[ ...same...

otherwise your array doesn't have a way of accounting for all ints,
since your array's element's index goes only up to 0xFFFFFFFE...

Victor
Jul 22 '05 #7

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:0U2Ab.433429$Tr4.1216305@attbi_s03...
"Sean Kenwrick" <sk*******@hotmail.com> wrote...

"J. Campbell" <ma**********@yahoo.com> wrote in message
news:b9**************************@posting.google.c om...
>
> i would consider the following quicker:
>
> #include <iostream>
> using namespace std;
>
> int man(int &x, int &y, int &z)
> {
> if (x>y && x>z) return x;
> if (y>x && y>z) return y;
> return z;
> }
>
> int main()
> {
> int a, b, c;
>
> cout << "type 3 numbers seperated by a space: ";
> cin >> a >> b >> c;
> cout << "maximun of those numbers = " << max(a,b,c) << endl;
>
> return 0;
> }
> ----
> but as i said, it's all a matter of nanoseconds; you don't have to
> worry.

If you really want to get minimalistic, you could go with:

#include <iostream>
int compare(int x, int y, int z){return x>y?x>z?x:z:y>z?y:z;}
int main(){
int x(1), y(-1), z(13);
std::cout << compare(x,y,z);
return 0;
}

of course, the line:
return x>y?x>z?x:z:y>z?y:z;
is really:
return ((x>y)?((x>z)?x:z):((y>z)?y:z));


If speed is the most important and you have unlimited memory you could
define an array as follows:

int max_of_3_integers[0xFFFFFFFF][0xFFFFFFFF][0xFFFFFFFF];

Then you initialize the array so that each element:
max_of_3_integers[x][y][z] holds the highest value of the three integers. Then you could have the macro:

#define max(a,b,c) max_of_3_integers[a][b][c]

to return the maximum integer value out of the three.

Unfortunately the memory required for the array would be 8^96 bytes and it would probably take a few years to initialise the array. But once the
initialisation had complete you would probably save a few CPU cycles each time it was called.... :-)


Unfortunately, this solution doesn't work. You need to do

int max_of_3_integers[1 << (std::numeric_limits<int>::digits() + 1)]
[ ...same...

otherwise your array doesn't have a way of accounting for all ints,
since your array's element's index goes only up to 0xFFFFFFFE...

Victor

Doh!

Actually I was assuming 32 bit integers in which case it should have been:

int max_of_3_integers[0x100000000][0x100000000][0x100000000];

But obviously your version is better since it is implementation independant.

However it is not altogether clear whether my solution would be quicker even
if a computer could be built to run it, since a 3d array lookup would
require something like 6 multiplications and three
additions:

int * address=max_of_3_integers+ a* (0x10000000 * 0x10000000 *
sizeof(int)) + b * (0x10000000 * sizeof(int)) + c * sizeof(int);

Plus the actual memory fetch.

This compares to something like 4 subtractions and four carry bit tests in
the original solutions..(quite a bit quicker).

But even though my solution won't run on any computer that exists today and
would require several years to initialise (perhaps hundreds of years), and
it would also run slower - If you ignore the initialisation code it is
definitely far more readable!

Oh well I better not give up my day job :-)
Shit, this is my day job :-(

Sean

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.