What is the most efficient way to write a funtion (InchesToYards), expecting inches and returns yards, using the following prototypes:
int inchesToFeet (int inches);
int FeetToYards (int feet);
Printable View
What is the most efficient way to write a funtion (InchesToYards), expecting inches and returns yards, using the following prototypes:
int inchesToFeet (int inches);
int FeetToYards (int feet);
do you mean you already have those functions? If so, then this would be the easiest:
But it would be faster in execution to rewrite the conversion.Code:int inchesToYards(int inches)
{
return FeetToYards(inchesToFeet(inches));
}
Not if you inline then :)Quote:
Originally posted by CornedBee
do you mean you already have those functions? If so, then this would be the easiest:
But it would be faster in execution to rewrite the conversion.Code:int inchesToYards(int inches)
{
return FeetToYards(inchesToFeet(inches));
}
even if he inlines them. I don't know inches, yards and feet, so I'll make a metric example:
A good compiler may optimize that away, but if not, the second would be slightly faster. It doesn't matter really, unless you're counting femtoseconds :)Code:inline int MmToM(int mm)
{ return mm*1000; }
inline int MToKm(int m)
{ return m*1000; }
// now what is faster?
int MmToKmA(int mm)
{
return MToKm(MmToM(mm);
}
// becomes:
// return mm*1000*1000;
int MmToKmB(int mm)
{
return mm*1000000;
}
Well, you've got at least 10 extra clock cycles, what with the pop, skip, and a jmp :rolleyes: :pQuote:
Originally posted by CornedBee
A good compiler may optimize that away, but if not, the second would be slightly faster. It doesn't matter really, unless you're counting femtoseconds :)
:D
Thanks!! you all... :)
I quess there are several ways to slice and dice this conversion...