[solved] time measure

hello,
Im trying to estimate the time needed to run some piece of code that has to do a calculation (and adaptation to specific limits) of the phase values on a three phase generator, on different rotor angles.

#include <math.h>

float angle1 = 40.0;
int ph1 = 0;
int ph2 = 0;
int ph3 = 0;
int count = 0;
unsigned long microsold = 0;
unsigned long deltamicros = 0;
String txtmsg = "";

void setup() {
   Serial.begin(115200);
}

void loop() {

  microsold = micros();
  
  ph1 = 2000 + int(2000 * sin(radians(angle1)));
  ph2 = 2000 + int(2000 * sin(radians(angle1 + 120)));
  ph3 = 2000 + int(2000 * sin(radians(angle1 + 240)));
  deltamicros = micros() - microsold;
    
  Serial.println(deltamicros);
 
 // Serial.print(ph3);
  //Serial.print(" ");
  //Serial.print(angle1);
  //Serial.print(" ");
  //Serial.println(count);
 
  count++;
  ReadSerial();
  
}

void ReadSerial(void) {
  int BytePos = 0;
  char inchar = ' ';
  if (Serial.available() > 0) {
    while (Serial.available() > 0)
    {
      inchar = Serial.read();
      txtmsg += inchar;
    }

    if (inchar == 10) {//line feed?
      if (txtmsg.substring(0, 5) == "angle")      {
        int intval = txtmsg.substring(5, txtmsg.indexOf(".") ).toInt();
        int decval = txtmsg.substring(txtmsg.indexOf(".") + 1, txtmsg.indexOf(".") + 2 ).toInt();
        angle1 = float(intval) + float(decval) / 10;
      }
      txtmsg = "";
    }
  }
}

As you can see,
ph1, ph2 and ph3 is the target code
deltamicros should be [ONLY] the calculation time
ReadSerial is a function to set the desired angle.

Calculation runs ok.
time estimate is strange. If ReadSerial() statement is NOT icluded, 4 or 8 usec is displayed. If ReadSerial() statement IS included, deltamicros time displayed is around 480usec.

Also, if I change a little the code and do a Serial.print(micros()) before and after calculation, the values are also strange, for example (as displayed at one time)
WITH ReadSerial :micros before 1925596 micros after 1926416 delta 820
WITHOUT ReadSerial :micros before 916336 micros after 916932 delta 596 (not 4 or 8)

Any ideas for the problem (or how can I do an acceptable measurement)?

If you don't change the input values, the compiler can (and does, it seems) optimise away the calculation.

Make the inputs "volatile"

That is the compiler optimizing for you :wink:

If you never call ReadSerial (bad name because you do a lot more then just reading serial ;)) angel1 (again, bad name, where is angle2? Or why isn't it a nice array) is never changing. The compiler notices that and tsays "fine, I will calculate ph1, ph2 and ph3 now". So the 480us is right. Doing float math is slow, doing sin calculations is slow. If you want fast, drop the float (float is NOT just a decimal point) and switch to a look up table. And while you're at it, drop String and use a string.

ok, it is really a compiler ...whatever

just added in the loop, a "angle1+=0.0000;" to get correct readings

thank you

Why didn't you just:

  1. Just include the function, because like we told you, the time is correct that way
  2. or just included the volatile keyword like TolpuddleSartre told you...