Calculation help

What is the best way to execute this in C?

rpm =  1/(9 * (elapsed / 60,000,000));

elapsed is an integer number of microseconds of 3,000 to 25,000
The Propeller is like cool and stuff..... uhhh.....huh huh.

Comments

  • Dave HeinDave Hein Posts: 5,844
    edited 2018-08-13 - 01:35:33
    rpm = 60000000/(9*elapsed);
  • Beavis3215Beavis3215 Posts: 224
    edited 2018-08-13 - 02:01:39
    Thanks again Dave, never made the connection. :)
    The Propeller is like cool and stuff..... uhhh.....huh huh.
  • jmgjmg Posts: 13,100
    or even
    rpm = 6666667/elapsed;
  • Always good programming practice to calculate the result of constants or variables that do not change within a loop before entering that loop. Makes for faster code.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • Though a good compiler is often better at doing this than you are, if allowed to...
  • Mark_T wrote: »
    Though a good compiler is often better at doing this than you are, if allowed to...

    True - I recently spent several hours attempting to speed up a couple of time sensitive procedures by simplifying calcs, etc. and the compiler (PropGCC) spit out pretty much the same assembly despite multiple approaches. One important concept is when scaling variables up or down to facilitate integer math using a power of 2 for the mults and divs is very, very fruitful when looking for speed. This has been mentioned multiple times over many years here but is worth repeating.

    Mike R...
Sign In or Register to comment.