Shop OBEX P1 Docs P2 Docs Learn Events
Calculation help — Parallax Forums

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

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2018-08-13 01:35
    rpm = 60000000/(9*elapsed);
  • Beavis3215Beavis3215 Posts: 229
    edited 2018-08-13 02:01
    Thanks again Dave, never made the connection. :)
  • jmgjmg Posts: 15,173
    or even
    rpm = 6666667/elapsed;
  • kwinnkwinn Posts: 8,697
    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.
  • 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.