Pages: [1]   Go Down
Author Topic: Measuring execution time of functions without Serial.print  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 241
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Rapa Nui
Offline Offline
Edison Member
*
Karma: 60
Posts: 2066
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 36
Posts: 1219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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();
Logged

Rob Tillaart

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

Offline Offline
Edison Member
*
Karma: 36
Posts: 1219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8473
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 65
Posts: 2111
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -
Logged

Rob Tillaart

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

USA
Online Online
Jr. Member
**
Karma: 4
Posts: 89
If you can't fix it with a hammer, it must be an electrical problem.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this code profiler help?
Logged


Pages: [1]   Go Up
Jump to: