Utility class to print elapsed time

Hi all, I've put together this simple class that eases the task of measuring elapsed time and printing it via Serial for debug purposes.

Usage:

TimeUtils tutils;

// ...for functions that require milliseconds to execute
tutils.mark();
slow_function();
tutils.printElapsed();

// ...for functions that execute in less than a ms
tutils.mark(TimeUtils::MICROS);
fast_function();
tutils.printElapsed();

Notice that the class remembers the resolution set at mark time, and automatically adjusts the output of printElapsed() accordingly. If called without arguments, mark() defaults to MILLIS resolution. A mark() call overwrites the previous one. printElapsed() will print exactly the same string until the next mark() call.

I hope this 10 minutes hack will be helpful to somebody. Comments and critics are obviously welcome.

TimeUtils.h

#ifndef _TIMEUTILS_H

#if ARDUINO >= 100
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif

class TimeUtils {
public:
    enum Resolution { MILLIS, MICROS };
    TimeUtils() { t = 0; res = MILLIS; };
    void mark(enum Resolution r = MILLIS);
    void printElapsed();
private:
    unsigned long t;
    enum Resolution res;
};

#endif

TimeUtils.cpp

#include "TimeUtils.h"

void TimeUtils::mark(Resolution r) {
    switch(r) {
        case MILLIS:
            t = millis();
            break;
            
        case MICROS:
            t = micros();
            break;
    }
    res = r;
}

void TimeUtils::printElapsed() {
    switch(res) {
        case MILLIS:
            Serial.print(millis() - t);
            Serial.print("ms");
            break;
            
        case MICROS:
            Serial.print(micros() - t);
            Serial.print("us");
            break;
    }
    Serial.print(" elapsed");
}

I like it, especially the included enum.

I was thinking of how to handle both micros and millis in my stopwatch Class - http://www.arduino.cc/playground/Code/StopWatchClass - and I think you'r code shows a nice answer to that.

Well done!

Thank you.

Your class has definitely much better names than mine... I'm thinking about how to integrate my idea about resolution (millis vs. micros) into your code. If you don't mind, of course :-)