Timer

Hello all! I'm not looking to do anything with interrupts, but rather I'm looking to create some code that will time how long it takes for the Arduino to run through a set of readings and print that time to the serial monitor. I'm not quite sure how to go about doing this - any suggestions? I'm working with a prototype device here that is supposed to compete with commercially-available devices in terms of speed and I'd like to have a basis for comparison.

Thanks,

Kevin

I usually just insert Serial.println(millis(), DEC) at the points of interest. Only as accurate as the crystal/resonator, but usually plenty good for slide-rule accuracy. If I'm lazy and don't want to subtract, or just want to get fancy, I'll trap the start time with a variable, something like

    unsigned long startTime;
    startTime = millis();
    //code to be timed here
    Serial.print(millis() - startTime, DEC);
    Serial.println(" ms");

If the operation being timed is very short, micros() is made-to-order (so to speak) for measuring orders of magnitude shorter durations.

Jack and Paul, thanks for your help! I didn't know that you could use millis() to time things with the Arduino! That's a neat trick that I'm sure will come in handy later on. My readings look very, very good :D

I did a timer class a little while back:

http://www.gammon.com.au/forum/?id=11093

With that installed you can do something like this:

#include <Timer.h>

void myfunction ()
  {
  Timer t (__PRETTY_FUNCTION__);

  //  do something lengthy here
  
  }

The constructor remembers the current time, the destructor displays how long it took. So basically you can time a function (or any batch of code) with one line of code.