Results 1 to 6 of 6

Thread: Convertions in C++

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22

    Question Convertions in C++

    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);
    Eena

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    do you mean you already have those functions? If so, then this would be the easiest:
    Code:
    int inchesToYards(int inches)
    {
      return FeetToYards(inchesToFeet(inches));
    }
    But it would be faster in execution to rewrite the conversion.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by CornedBee
    do you mean you already have those functions? If so, then this would be the easiest:
    Code:
    int inchesToYards(int inches)
    {
      return FeetToYards(inchesToFeet(inches));
    }
    But it would be faster in execution to rewrite the conversion.
    Not if you inline then
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    even if he inlines them. I don't know inches, yards and feet, so I'll make a metric example:
    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;
    }
    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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Thanks!! you all...

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width