473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

which program will run better:)

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
7 1833
"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
> /*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
<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
>
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

"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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul | last post by:
I am creating a Program for college, in which the Program will read a Folder and create a HTML page from the pictures that are storrd in that folder. . What would be the best way to do...
14
by: Hafez | last post by:
Hi there every body I'm new in programming in windows. I know how to program with C and C++ in DOS but I don't know select which one for programming in windows: Visual C++ ,C# or Delphi. ...
22
by: smartwolf agassi via DotNetMonster.com | last post by:
I'm a C# language learner. I want to know which IDE is better for C# programing, Borland C#Builder or VS.net 2003? -- Message posted via http://www.dotnetmonster.com
1
by: Clifford Easden | last post by:
I wrote a program in VB6 for the local library, which catalogs all pertinent information on videos inventory, on hand, checked out etc. The program has works flawlessly on the six computers used...
5
by: Peggy Wu | last post by:
dear all, I am a programmer ,coding in IBM COBOL and the DB is DB2 V6 on OS/390. As below, for the performance issue, Does anyone know which one is better ?? To get a record that field A has a...
15
by: Rob Meade | last post by:
Hi all, I have a databse which I'm pulling the data from for my ASP page. I have 4 tables, Course, Feature, Objective, and PreRequisite. The last three all contain a course product code and a...
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
84
by: Patient Guy | last post by:
Which is the better approach in working with Javascript? 1. Server side processing: Web server gets form input, runs it into the Javascript module, and PHP collects the output for document prep....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.