Go Down

Topic: Measuring execution time of functions without Serial.print (Read 306 times) previous topic - next topic

boylesg

I figured out a solution to that other problem I was having with Serial.println distorting the execution time of functions.

I put 14 LEDs on a prototype shield and connected them to 14 digital pins.

Using digitalFastWrite(...) I can then output the difference in calls to micros(), before and after the function, as a binary number on the LEDs.

With this I got an execution time, of one particular function, of around 600us rather than 6-8ms when I was trying to use Serial.print.

pito

You can send the time value via SPI to an external 16bit shift register. You may need maybe 5 usecs to do that...

Caltoa

There is timing measurement somewhere in this sketch: http://playground.arduino.cc/Main/ShowInfo
You could add such a section for timing of the Serial.print().

Serial.print puts the character in a buffer. But if the buffer is full, the Serial.print waits until there is a free place in the buffer. That will slow things down a lot.

robtillaart

If you just want tp measure the time of a function you could do the following

Code: [Select]

start = micros();
for (int i=0; i< 1000; i++)
{
   x = f();
}
t1 = micros() - start;

start = micros();
for (int i=0; i< 1000; i++)
{
   x = f();
  x = f();
}
t2 = micros() - start;
Serial.println(t2 - t1);

this way you subtract the all overhead (excl. interrupts) of the loop to get the time of  f();
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Caltoa

That's a very nice solution.
But 1000 Serial.print will not work. Will it work with only 10 Serial.print ?
The buffer is 64 bytes (I think), so that will be 20 each time.

I suggest:
- wait until all serial transfer has finished.
- run 20 times Serial.print in loop with micros()
- run 20 times two Serial.prints in loop with micros()

Graynomad

Buy a logic analyser and pulse a pin. Job done.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

pYro_65


Buy a logic analyser and pulse a pin. Job done.

______
Rob


Yes that is kind of what I do.

For a project that had to have minimal impact I simply toggled a pin which was connected to another Uno. It recorded the time between a high and low. It also removed the overhead of the serial lib and could monitor multiple pins/ timing jobs.

robtillaart


That's a very nice solution.
But 1000 Serial.print will not work. Will it work with only 10 Serial.print ?
The buffer is 64 bytes (I think), so that will be 20 each time.

Maybe not in exact identical code but the principle is sound. Another trick involves using timers directly,
I measured serial print to optimize the performance of printing float's - http://forum.arduino.cc/index.php?topic=179111.60 -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

JimEli


Go Up