PDA

Click to See Complete Forum and Search --> : Convertions in C++


Eena
Nov 10th, 2001, 10:30 PM
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);

CornedBee
Nov 11th, 2001, 05:36 AM
do you mean you already have those functions? If so, then this would be the easiest:

int inchesToYards(int inches)
{
return FeetToYards(inchesToFeet(inches));
}

But it would be faster in execution to rewrite the conversion.

kedaman
Nov 11th, 2001, 05:47 AM
Originally posted by CornedBee
do you mean you already have those functions? If so, then this would be the easiest:

int inchesToYards(int inches)
{
return FeetToYards(inchesToFeet(inches));
}

But it would be faster in execution to rewrite the conversion.
Not if you inline then :)

CornedBee
Nov 11th, 2001, 12:16 PM
even if he inlines them. I don't know inches, yards and feet, so I'll make a metric example:

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;
}

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 :)

parksie
Nov 11th, 2001, 12:29 PM
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 :) Well, you've got at least 10 extra clock cycles, what with the pop, skip, and a jmp :rolleyes: :p

:D

Eena
Nov 11th, 2001, 02:11 PM
Thanks!! you all... :)

I quess there are several ways to slice and dice this conversion...